Express cheatsheet
Express cheatsheet
A quick reference cheatsheet for Express, a flexible and streamlined web framework for
Node.js
# Getting Started
Hello World express -h express()
$ node index.js
Attribute
var express = require("express"); app.get("/", function (req, res) {
var app = express(); req.app # console.dir(res.headersSent);
//false
console.dir(app.locals.title); req.baseUrl # res.send("OK");
//=> 'My App' console.dir(res.headersSent); //true
console.dir(app.locals.email); req.body # });
//=> 'me@myapp.com'
req.cookies # Attribute
Attribute
req.fresh # res.app #
Local variables in the
app.locals
application # req.hostname # res.headersSent #
app.enabled() #
req.signedCookies # res.get() #
app.engine() #
req.stale # res.json() Send JSON response #
app.get(name) #
req.subdomains # Send a response with
res.jsonp()
JSONP support #
app.get(path, callback) #
req.xhr #
res.links() #
app.listen() # Method
res.location() #
app.METHOD() # req.accepts() #
res.set() #
app.set() # req.param() #
app.use() # req.range() # res.status() #
res.type() #
# Example
- Router - Response - Request
Called for any request passed to this router The res object represents the HTTP response A req object represents an HTTP request and
sent by the Express application when it has properties for the request query string,
router.use(function (req, res, next) { receives an HTTP request parameters, body, HTTP headers, etc.
//.. some logic here .. like any
other middleware app.get("/user/:id", (req, res) => { app.get("/user/:id", (req, res) => {
next(); res.send("user" + req.params.id); res.send("user" + req.params.id);
}); }); });