How to server specifc static files with express.js

If you have this setup /app /public/index.html /media Then this should get what you wanted var express = require(‘express’); //var server = express.createServer(); // express.createServer() is deprecated. var server = express(); // better instead server.configure(function(){ server.use(‘/media’, express.static(__dirname + ‘/media’)); server.use(express.static(__dirname + ‘/public’)); }); server.listen(3000); The trick is leaving this line as last fallback server.use(express.static(__dirname + … Read more

Creating a expressjs middleware that accepts parameters

function HasRole(role) { return function(req, res, next) { if (role !== req.user.role) res.redirect(…); else next(); } } I also want to make sure that I don’t make multiple copies of the same function: function HasRole(role) { return HasRole[role] || (HasRole[role] = function(req, res, next) { if (role !== req.user.role) res.redirect(…); else next(); }) }

HtmlWebpackPlugin injects relative path files which breaks when loading non-root website paths

Try setting the publicPath in your webpack config: output.publicPath=”https://stackoverflow.com/” HtmlWebpackPlugin use the publicPath to prepend the urls of the injects. Another option is to set the base href in the <head> of your html template, to specify the base url of all relative urls in your document. <base href=”http://localhost:3000/”>

What are express.json() and express.urlencoded()?

Here is the explanation that should clear doubts on express.json() and express.urlencoded() and the use of body-parser. It took me some time to figure this out. What is Middleware? It is those methods/functions/operations that are called BETWEEN processing the Request and sending the Response in your application method. When talking about express.json() and express.urlencoded() think … Read more

How do I setup a SSL certificate for an express.js server?

See the Express docs as well as the Node docs for https.createServer (which is what express recommends to use): var privateKey = fs.readFileSync( ‘privatekey.pem’ ); var certificate = fs.readFileSync( ‘certificate.pem’ ); https.createServer({ key: privateKey, cert: certificate }, app).listen(port); Other options for createServer are at: http://nodejs.org/api/tls.html#tls_tls_createserver_options_secureconnectionlistener