what's the equiv part of hapi request.state for express?

825 views Asked by At

In hapi, there was a state obj for request request.state.

What's the equivalent part in express? req.session?

Thanks

1

There are 1 answers

0
kachhalimbu On BEST ANSWER

Not familiar with Hapi but from docs I understand state corresponds to http cookies. If my understanding is correct, then in expressjs you can use cookie-parser middleware to get the cookies sent by client using req.cookies

var express      = require('express')
var cookieParser = require('cookie-parser')

var app = express()
app.use(cookieParser())

app.get('/', function(req, res) {
  console.log("Cookies: ", req.cookies)
})

app.listen(8080)

If you want to set state or in other words cookie then just set it on the response i.e. res like this. Reference

var cookieConfig = {}; // set config options here like domain, httpOnly, maxAge etc.
res.cookie('key', 'value', cookieConfig);