Express
Express is built on the top of the core module "connect", and has all its basic features, such as using the next() method , or the createServer() method, or the use() for middle-ware.
All connect middle-ware can be used with express, but not all express middle-ware can be used with connect, this is since express modifies the reques & response objects.
Popular connect/express middle-ware
serve-static
npm install serve-static
included with express using the .static() method.
Sets a server to serve as the static resources directory.
Excellent header support, for example sets headers automatically for 200, 304 (not modified), 400, 500
has an optional secondary object param, that includes an index property, which is an array of permitted files to serve. Example:
app.use(serveStatic(__dirname + '/public', {'index': ['default.html', 'default.htm']}))
serve-index
npm install serve-index.
by default serves and Html page with the listings of a directory.
if the .static() middle-ware is used previously, it will enable the user to get the index.html file before getting the directory listing.
body-parser
parse string request bodies into javascript objects.
attaches parsed body to the request.body object.
raises error on invalid JSON string sent to the server as request body, an error which can be handled using error handling middle-ware.
cookie-parser
Cookies: a string of data sent to client, for the client to use, which sometimes can be modified on the client and sent back to the server, in the requests header, under the Cookie header. Cookies are a way to keep client state on server since Http is a stateless protocol. Cookies are stored on the browser and are domain specific, meaning only the domain that sent the document can access the cookies it set.
By default express has built in feature to read cookies from the header:
request.headers['cookie'].
By default express has a built in feature to write cookies into the response header using res.cookie(key,value).
will provide a response.cookie(key,value) method, for easier
Adding in cookie-parser into the express queue:
will parse the request.headers into a javascript object and attach it to request.cookies
Cookie signing :
Since cookies are stored on the client, they can be forged by 3rd side malicious client scripts & by server side hackers that realize how the cookie generation logic works on the server side, this is even though cookies are domain specific by default as part of the browsers CORS policy. Techniques of cookie forging is not covered here.
Digital signature - digitally signed cookies assure that the cookies won't be forged, and cookie parser allows us to do this.
H-MAC - H-MAC is a Key:Hash Message Authentication code, which is basically a secret key appended to the end of the server generated cookie. the secret is basically a hash made of some secret parameters known only to the server. This secret-key hash is checked on the server side with each request, and if the hash sent from the client does not match the hash generated by the server the request is denied and discarded.
Digitally signing with cookie parser
Creation of a secret key to use with signed cookies:
To assign a key to be used with cookies, you need to pass the secret hash string to the cookie-parser creation function. e.g:
express().use(cookieParser('my super secret sign key'))
Reading signed cookies:
If the request contains signed cookies you can access them on the request object under the signedCookies object as show below:
request.signedCookies.name
Creating a signed cookie to send with the response:
To create a signed cookies, you can pass an options object to the response.cookie(key,value,{options...}) function as shown below:
res.cookie('name', 'foo', { signed: true });
example for the combination of all 3:
.use(cookieParser('my super secret sign key'))
.use('/toggle', function (req, res) {
if (req.signedCookies.name) {
res.clearCookie('name');
res.end('name cookie cleared! Was:' + req.signedCookies.name);
}
else {
res.cookie('name', 'foo', { signed: true });
res.end('name cookie set!');
}
})
.listen(3000);
Http only cookies
By default the browser gets access to cookies using javascript via the document.cookie object, this makes cookies vulnerable to Cross-Side-Scripting (XSS), for this case there is a possibility to deny javascript access to cookies, using the HttpOnly flag, added to the Set-Cookie header.
This can be done by providing the options object, to-the response object's cookie method, and defining the httpOnly attribute to true
A little bit on XSS (Cross-side-Scripting)
XSS is when some malicious user manages to inject
JavaScript into your web-site content, it allows that JavaScript to read cookies that might contain sensitive information for the currently logged in user and ship it to a malicious web site.
A little about HTTPS: