Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Pass Data Between Routes in Express

Suppose I have this POST route which receives some data.

app.post('/getData', function(req, res){
  var retrievedData = req.body.exampleVariable;
   // Send data to GET method
});

And I have this GET method which renders a page, but needs the data I retrieved in the POST method

app.get('/displayData', function(req, res){
  // Retrieve data from POST method and display it.
  res.render('/examplePage.ejs', {retrievedData : req.retrievedData});
});

What is the best way to pass the retrievedData variable from the given POST route to the GET route?

As a side note, the res.render() method only seems to work in app.get() type methods

like image 335
Perturbative Avatar asked Jan 14 '16 18:01

Perturbative


People also ask

How do I use routing in express?

Routing. Routing refers to how an application’s endpoints (URIs) respond to client requests. For an introduction to routing, see Basic routing. You define routing using methods of the Express app object that correspond to HTTP methods; for example, app.get() to handle GET requests and app.post to handle POST requests.

What are routes in ExpressRoute?

Route methods. A route method is derived from one of the HTTP methods, and is attached to an instance of the express class. The following code is an example of routes that are defined for the GET and the POST methods to the root of the app.

How do I create routes for a wiki in express?

First we create routes for a wiki in a module named wiki.js. The code first imports the Express application object, uses it to get a Router object and then adds a couple of routes to it using the get () method. Last of all the module exports the Router object. Note: Above we are defining our route handler callbacks directly in the router functions.

How do I use the router module in my Express application?

To use the router module in our main app file we first require () the route module ( wiki.js ). We then call use () on the Express application to add the Router to the middleware handling path, specifying a URL path of 'wiki'. The two routes defined in our wiki route module are then accessible from /wiki/ and /wiki/about/.


1 Answers

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.

like image 171
Ryan White Avatar answered Oct 19 '22 12:10

Ryan White