Snelle referentie

Express.js Spiekbrief

Snelle referentiegids voor het Express.js webframework: applicatie-instellingen, middleware-configuratie, request-eigenschappen en response-methoden.

· Gepubliceerd

Express is een snel, flexibel en minimalistisch webframework voor Node.js.

Instellingen (Settings)

app.set('x', 'yyy')
app.get('x') //=> 'yyy'

app.enable('trust proxy')
app.disable('trust proxy')

app.enabled('trust proxy') //=> true

Uitvoeromgeving (Env)

app.get('env')

Configuratie

app.configure('production', function() {
  app.set(...)
})

Middleware

app.use(express.static(__dirname + '/public'))
app.use(express.logger())

Lokale helpers (Helpers)

app.locals({
  title: "MyApp"
})

Verzoek en Antwoord (Request & Response)

Request-object

// GET /user/tj
req.path         //=> "/user/tj"
req.url          //=> "/user/tj"
req.xhr          //=> true|false
req.method       //=> "GET"
req.params
req.params.name  //=> "tj"
req.params[0]
// GET /search?q=tobi+ferret
req.query.q      //=> "tobi ferret"
req.cookies
req.accepted
// [ { value: 'application/json', quality: 1, type: 'application', subtype: 'json' },
//   { value: 'text/html', quality: 0.5, type: 'text', subtype: 'html' } ]
req.is('html')
req.is('text/html')
req.headers
req.headers['host']
req.headers['user-agent']
req.headers['accept-encoding']
req.headers['accept-language']

Response-object

res.redirect('/')
res.redirect(301, '/')
res.set('Content-Type', 'text/html')
res.send('hi')
res.send(200, 'hi')
res.json({ a: 2 })

Verwante onderwerpen