Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the "expires" header in ExpressJS?

I tried the following, but the expiration is set to 1 minute:

app.get(['/css/*','/js/*'],express.static('public',{maxAge:7*86400000}));
app.get(['/fonts/*'],express.static('public',{maxAge:30*86400000}));

How do set the expiration time using ExpressJS? In the code above, I tried setting the expiration time to 1 week and 1 month respectively.

like image 237
Leo Jiang Avatar asked Jul 23 '15 06:07

Leo Jiang


1 Answers

You use Express static, and it's perfecly fine, it's rather powerfull tool to serve static files.

express.static is the only built-in middleware in Express. It is based on serve-static, and is responsible for serving the static assets of an Express application.

Besides maxage support it also supports ETags.

Just use it this way:

app.use(express.static(__dirname + '/public', { maxAge: '1d' }));

Here is the very good explanation.

like image 184
Alexander Arutinyants Avatar answered Oct 30 '22 03:10

Alexander Arutinyants