Skip to content

Instantly share code, notes, and snippets.

@jfromaniello
Created April 16, 2014 15:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jfromaniello/10898647 to your computer and use it in GitHub Desktop.
Save jfromaniello/10898647 to your computer and use it in GitHub Desktop.
//very very bad:
app.use(express.cookieParser())
app.use(express.session({ secret: 'keyboard cat', key: 'sid', cookie: { secure: true }, store: blabla}))
app.use(express.static(__dirname + '/public'));
//good:
app.use(express.static(__dirname + '/public'));
app.use(express.cookieParser())
app.use(express.session({ secret: 'keyboard cat', key: 'sid', cookie: { secure: true }, store: blabla}))
/**
This is better because it doesn't:
- parse the cookie
- fetch the session (important if you use mongo/redis/etc)
**/
/** BONUS TIP:
This bit me a few times. Browsers make requests for "favicon.ico",
if you do not have that file `static` does "next()" and you end up with
a Set-Cookie header on a 404 to /favicon.ico
So, better you return 404 before the cookie and session middlewares for broken static assets.
One way is to use the "onlyStatic" middleware https://gist.github.com/jfromaniello/10021643 as follows:
**/
app.use(express.static(__dirname + '/public'));
app.use(onlyStatic(function (req, res, next) {
res.send(404);
}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment