K
K
kaleon2019-05-23 03:57:50
JavaScript
kaleon, 2019-05-23 03:57:50

How to save session declared in POST route?

The session only works in the route within which it was declared.
I use Express-session, everything works fine in GET routes.
Cookie-Parser has the same problem.

router.post('/new/', (req,res,next) => {

  const data = {id: req.body.id, name: req.body.name}
 
   req.session.cart.newItem = data
   req.session.save()  
})

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey, 2019-05-24
@kaleon

Try to return the response in the request handler and everything will be fine. You in your implementation 504 Gateway Timeout do.

router.post('/new/', (req,res,next) => {
  const data = {id: req.body.id, name: req.body.name};
  req.session.cart.newItem = data;
  res.status(200).send(true); // к примеру
})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question