Debugging Express

Express uses the debug module internally to log information about route matches, middleware functions that are in use, application mode, and the flow of the request-response cycle. debug is like an augmented version of console.log, but unlike console.log, you don’t have to comment out debug logs in production code. Logging is turned off by default and can be conditionally turned on by using the DEBUG environment variable. To see all the internal logs used in Express, set the DEBUG environment

res.send()

res.send([body]) Sends the HTTP response. The body parameter can be a Buffer object, a String, an object, or an Array. For example: res.send(new Buffer('whoop')); res.send({ some: 'json' }); res.send('<p>some html</p>'); res.status(404).send('Sorry, we cannot find that!'); res.status(500).send({ error: 'something blew up' }); This method performs many useful tasks for simple non-streaming responses: For example, it automatically assigns the Content-Length HTTP response header field

res.type()

res.type(type) Sets the Content-Type HTTP header to the MIME type as determined by mime.lookup() for the specified type. If type contains the “/” character, then it sets the Content-Type to type. res.type('.html'); // => 'text/html' res.type('html'); // => 'text/html' res.type('json'); // => 'application/json' res.type('application/json'); // => 'application/json' res.type('png'); // => image/png:

res.location()

res.location(path) Sets the response Location HTTP header to the specified path parameter. res.location('/foo/bar'); res.location('http://example.com'); res.location('back'); A path value of “back” has a special meaning, it refers to the URL specified in the Referer header of the request. If the Referer header was not specified, it refers to “/”. Express passes the specified URL string as-is to the browser in the Location header, without any validation or manipulation, except in case of back.

Events

Events

res.append()

res.append(field [, value]) res.append() is supported by Express v4.11.0+ Appends the specified value to the HTTP response header field. If the header is not already set, it creates the header with the specified value. The value parameter can be a string or an array. Note: calling res.set() after res.append() will reset the previously-set header value. res.append('Link', ['<http://localhost/>', '<http://localhost:3000/>']); res.append('Set-Cookie', 'foo=bar; Path=/; HttpOnly'); re

req.originalUrl

req.originalUrl req.url is not a native Express property, it is inherited from Node’s http module. This property is much like req.url; however, it retains the original request URL, allowing you to rewrite req.url freely for internal routing purposes. For example, the “mounting” feature of app.use() will rewrite req.url to strip the mount point. // GET /search?q=something req.originalUrl // => "/search?q=something"

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' });

app.use()

app.use([path,] function [, function...]) Mounts the specified middleware function or functions at the specified path. If path is not specified, it defaults to “/”. A route will match any path that follows its path immediately with a “/”. For example: app.use('/apple', ...) will match “/apple”, “/apple/images”, “/apple/images/news”, and so on. Note that req.originalUrl in a middleware function is a combination of req.baseUrl and req.path, as shown in the following example. app.use('/admin', f

app.set()

app.set(name, value) Assigns setting name to value, where name is one of the properties from the app settings table. Calling app.set('foo', true) for a Boolean property is the same as calling app.enable('foo'). Similarly, calling app.set('foo', false) for a Boolean property is the same as calling app.disable('foo'). Retrieve the value of a setting with app.get(). app.set('title', 'My Site'); app.get('title'); // "My Site" Application Settings The following table lists application settings. Not