Serving static files

In any web application, there is always a need to serve static files. Fortunately, Express' only built-in middleware is the express.static() middleware, which provides this feature. To add static file support to the previous example, just make the following changes in your config/express.js file:

const express = require('express'); const morgan = require('morgan'); const compress = require('compression'); const bodyParser = require('body-parser'); const methodOverride = require('method-override'); module.exports = function() { const app = express(); if (process.env.NODE_ENV === 'development') { app.use(morgan('dev')); } else if (process.env.NODE_ENV === 'production') { app.use(compress()); } app.use(bodyParser.urlencoded({ ...

Get MEAN Web Development - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.