How to Pass Data Between Routes in Express How to Pass Data Between Routes in Express express express

How to Pass Data Between Routes in Express


You can use

app.post('/getData', function(req, res){  app.set('data', req.body.exampleVariable);});app.get('/displayData', function(req, res) {  res.render('/examplePage.ejs', {retrievedData : app.get('data')});});

but there is no guarantee that the returned data will be the data that the user set, and the data won't be sent until a user makes a request to get route. This is also making your express server stateful, which comes with a lot of possible disadvantages.

You can read a bit about it here

If you want your app to be stateless, you can instead pass your to somewhere external, like a remote server like mysql which is independant of the express server.

In case it is of any use, you can also send and receive data in the same request if you prefer, as in your comment this seems to be what you are trying to do.If you have something like below, it should be working fine, as long as you are definitely making a "POST" request to "/data", considering it was working fine for "GET" requests.

app.post('/data', function(req, res){  res.render('/examplePage.ejs', {    retrievedData: req.body.exampleVariable  });});

Below is an example express app to show how this might work

form.jade

form(method="post" action="data")  input(type="text" name="data" value="some data")  input(type="submit" value="submit")

data.jade

h1=data

server.js

require('express')()  .use(require('body-parser').urlencoded({ extended: false }))  .get('/form', (req, res) => res.render('form.jade'))  .post('/data', (req, res) => res.render('data.jade', { data: req.body.data }))  .listen(8083);

If you start the server and go to "localhost:8083/form",after submitting the form you are shown a rendered page containing the data that you posted.


I am posting this assuming you are running express.js/node.js. All directories are in root directory.

I think you can just use one route instead of two routes. Assuming you have the javascript function/method to get data in a directory under root directory call getModules and your method is stored in a file call getData.js.

routes/index.js

var getDataModule = require('../getModules/getData.js');router.post('/getData', function(req, res) {  getDataModule.gatData(req.body, function(data) {    res.render('/displayData', { title: 'Title', results: data });  });});

Then your function should be export the data. Otherwise it will not with this way. The getData.js can be like this.

getModules/getData.js

module.exports.getData = function(data, callback){  //body that work on data or what ever the logic including a callback}

Then you can render the results in displayData.ejs which is similar to normal HTML page. But if you are planning to use this diplayData.ejs as a template you can include examplePage.ejs inside the body.

Code Explanation

With this coding, you may send something with req.body to your function or not. But the returning value will be sent with data object. Then that is given to your next route which is going to render the data as HTML. Things that you're going to display is in results object/array. You can access to properties of the object as <%= results._source.prop1 %> and so on. But you need to know the identifier of the property.