Using template engines with Express

A template engine enables you to use static template files in your application. At runtime, the template engine replaces variables in a template file with actual values, and transforms the template into an HTML file sent to the client. This approach makes it easier to design an HTML page. Some popular template engines that work with Express are Pug, Mustache, and EJS. The Express application generator uses Pug as its default, but it also supports several others. See Template Engines (Express wi

req.acceptsCharsets()

req.acceptsCharsets(charset [, ...]) Returns the first accepted charset of the specified character sets, based on the request’s Accept-Charset HTTP header field. If none of the specified charsets is accepted, returns false. For more information, or if you have issues or concerns, see accepts.

Using middleware

Express is a routing and middleware web framework that has minimal functionality of its own: An Express application is essentially a series of middleware function calls. Middleware functions are functions that have access to the request object (req), the response object (res), and the next middleware function in the application’s request-response cycle. The next middleware function is commonly denoted by a variable named next. Middleware functions can perform the following tasks: Execute any co

res.clearCookie()

res.clearCookie(name [, options]) Clears the cookie specified by name. For details about the options object, see res.cookie(). res.cookie('name', 'tobi', { path: '/admin' }); res.clearCookie('name', { path: '/admin' });

Routing

Routing refers to the definition of application end points (URIs) and how they respond to client requests. For an introduction to routing, see Basic routing. The following code is an example of a very basic route. var express = require('express'); var app = express(); // respond with "hello world" when a GET request is made to the homepage app.get('/', function(req, res) { res.send('hello world'); }); Route methods A route method is derived from one of the HTTP methods, and is attached to a

res.set()

res.set(field [, value]) Sets the response’s HTTP header field to value. To set multiple fields at once, pass an object as the parameter. res.set('Content-Type', 'text/plain'); res.set({ 'Content-Type': 'text/plain', 'Content-Length': '123', 'ETag': '12345' }); Aliased as res.header(field [, value]).

app.disabled()

app.disabled(name) Returns true if the Boolean setting name is disabled (false), where name is one of the properties from the app settings table. app.disabled('trust proxy'); // => true app.enable('trust proxy'); app.disabled('trust proxy'); // => false

app.route()

app.route(path) Returns an instance of a single route, which you can then use to handle HTTP verbs with optional middleware. Use app.route() to avoid duplicate route names (and thus typo errors). var app = express(); app.route('/events') .all(function(req, res, next) { // runs for all HTTP verbs first // think of it as route specific middleware! }) .get(function(req, res, next) { res.json(...); }) .post(function(req, res, next) { // maybe add a new event... });

app.engine()

app.engine(ext, callback) Registers the given template engine callback as ext. By default, Express will require() the engine based on the file extension. For example, if you try to render a “foo.pug” file, Express invokes the following internally, and caches the require() on subsequent calls to increase performance. app.engine('pug', require('pug').__express); Use this method for engines that do not provide .__express out of the box, or if you wish to “map” a different extension to the templat

Events

Events