Flash messages for REST API

Viewed 454

I want to build REST API using Express. Aside from resources there are endpoints for authentication stuff like signup, signin and user option (the view templates are loaded). It's common practice to use sessions for messages but how can I handle this for REST API? IS it okay to use for example express-session for flash messages only or I need to resolve this without session?

1 Answers

Yes you can use express-session.

However you should not that if you wish to use this in production you'll need to setup a database of sort to store these sessions as it is not recommended to use the default in-memory storage solution.

As for the REST API part, just make sure that you call:

app.use(session({
  secret: 'keyboard cat',
  resave: false,
  saveUninitialized: true,
  cookie: { secure: true }
}))

Before you add your routes to the app. This way the session will be activated for all REST API calls. You can see a nice blog / example here which should help guide you through it.

Related