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

Hello world example

This is essentially going to be the simplest Express app you can create. It is a single file app — not what you’d get if you use the Express generator, which creates the scaffolding for a full app with numerous JavaScript files, Jade templates, and sub-directories for various purposes. First create a directory named myapp, change to it and run npm init. Then install express as a dependency, as per the installation guide. In the myapp directory, create a file named app.js and add the following

res.sendFile()

res.sendFile(path [, options] [, fn]) res.sendFile() is supported by Express v4.8.0 onwards Transfers the file at the given path. Sets the Content-Type response HTTP header field based on the filename’s extension. Unless the root option is set in the options object, path must be an absolute path to the file. The following table provides details on the options parameter. Property Description Default Availability maxAge Sets the max-age property of the Cache-Control header in milliseconds or a

res.render()

res.render(view [, locals] [, callback]) Renders a view and sends the rendered HTML string to the client. Optional parameters: locals, an object whose properties define local variables for the view. callback, a callback function. If provided, the method returns both the possible error and rendered string, but does not perform an automated response. When an error occurs, the method invokes next(err) internally. The local variable cache enables view caching. Set it to true, to cache the view

req.cookies

req.cookies When using cookie-parser middleware, this property is an object that contains cookies sent by the request. If the request contains no cookies, it defaults to {}. // Cookie: name=tj req.cookies.name // => "tj" For more information, issues, or concerns, see cookie-parser.

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:

router.all()

router.all(path, [callback, ...] callback) This method is just like the router.METHOD() methods, except that it matches all HTTP methods (verbs). This method is extremely useful for mapping “global” logic for specific path prefixes or arbitrary matches. For example, if you placed the following route at the top of all other route definitions, it would require that all routes from that point on would require authentication, and automatically load a user. Keep in mind that these callbacks do not h

req.baseUrl

req.baseUrl The URL path on which a router instance was mounted. The req.baseUrl property is similar to the mountpath property of the app object, except app.mountpath returns the matched path pattern(s). For example: var greet = express.Router(); greet.get('/jp', function (req, res) { console.log(req.baseUrl); // /greet res.send('Konichiwa!'); }); app.use('/greet', greet); // load the router on '/greet' Even if you use a path pattern or a set of path patterns to load the router, the base

Serving static files in Express

To serve static files such as images, CSS files, and JavaScript files, use the express.static built-in middleware function in Express. Pass the name of the directory that contains the static assets to the express.static middleware function to start serving the files directly. For example, use the following code to serve images, CSS files, and JavaScript files in a directory named public: app.use(express.static('public')); Now, you can load the files that are in the public directory: http://loc

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