How to pass data from one route to another nodejs with expressjs

Table of Contents

I want to add a certain attribute to the request object and access the added it from another route after redirecting. The following code shows what I exactly need.

const express = require('express') const app = express() app.get('/test1',(req,res)=>{ console.log('test1') req.name = 'lahiru' res.redirect('/test2') }) app.get('/test2',(req,res)=>{ console.log('test2') let val = req.name console.log(val) }) app.listen(3000)

But when I sends a request to the '/test1', I get the following output.

test1 test2 undefined

I tried this with express-session, but log-returns the same 'undefined'. Can anyone please help. Thanks in advance.

Source: Stack Overflow