Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js express static server hangs on images- Chrome only

This is a really strange bug that has been bothering me for ages. I have a basic website that uses Express Static middleware in addition to separate routes that render Jade.

Here's my config

app.set('views', __dirname + '/views');
app.set('view engine', 'jade');

app.use(stylus.middleware({
    src      : __dirname + '/public',
    dest        : __dirname + '/public',
    compile : function(str, path)
    {
        return stylus(str)
        .set('filename', path)
        .set('compress', true) 
        .use(nib())
    }
}));


app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
app.use(cookieParser())
app.use(session({ secret: conf.sessionSecret, 
                        store: new RedisStore(),
                        saveUninitialized: true,
                        resave: true
}));

app.use(express.static(__dirname + '/public'))

Here's my issue. After precisely 5 refreshes in a single tab in Chrome, image files refuse to load. All other content is fine, just images and just in Chrome. In order to remedy this, you have to open a new tab. I've tested this in Firefox as well- no issues. This error doesn't just happen in this application- it's happened in many of my others as well.

As this project is currently in development, I'd rather not put in online as an example. Rest assured, this is completely reproducible. Any ideas?

UPDATE: I updated to Express 4 on the slim chance that updating might fix my issue. It did not. You can see a screen cast I made of the issue here.

UPDATE: To test if the issue was with static server, I modified my code to be like this:

app.get('/img/*', function (req,res)
{
    console.log(req.url);
    console.log(path.extname(req.url));
    if (imgExt.indexOf(path.extname(req.url)) != -1)
    {
        res.sendfile('./public/'+req.url);
    }
});

app.use(express.static(__dirname + '/public'))

This had no effect, so I'm thinking this issue is either client-side or a bug with Chrome.

like image 482
Jack Guy Avatar asked Jun 25 '14 21:06

Jack Guy


People also ask

What is Express in Node JS?

A Node.js framework, Express facilitates data in a server and includes rendering your static files on the client-side such as images, HTML, CSS, and JavaScript. If you’re new to Express, check out our Introduction to Express to get caught up on the basics.

How do I serve static files in express?

To serve static files such as images, CSS files, and JavaScript files, use the express.static built-in middleware function in Express. The function signature is:

What is __dirname in Node JS?

Note that, we have passed __dirname, which is a Node.js global variable to the path of currently running file. We can use the same method to serve our other files, contact.html and about.html, as well:

How do I start an express server from a Node Package?

Within your package.json, update your start script to include node and your index.js file. This will allow you to use the npm start command in your terminal to launch your Express server. To store your files on the client-side, create a public directory and include an index.html file along with an image. Your file structure will look like this:


Video Answer


1 Answers

After hours upon hours of debugging, I discovered this legacy bit of code buried in my servers.

app.get('/favicon.ico', function (req, res)
{
    res.writeHead(200, {'Content-Type': 'image/x-icon'} );
});

It was intended to prevent the double requests for a favicon by an older version of Express. Obviously I forgot to call res.end() at the end. What boggles my mind is that neither Firefox nor Internet Explorer kept this connection alive between refreshes, while Chrome did. I appreciate the dialogue everybody!

like image 53
Jack Guy Avatar answered Oct 12 '22 21:10

Jack Guy