如何在 Express 中的路由之间传递数据 [英] How to Pass Data Between Routes in Express

查看:20
本文介绍了如何在 Express 中的路由之间传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有这个接收一些数据的 POST 路由.

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
});

我有这个 GET 方法来呈现页面,但需要我在 POST 方法中检索到的数据

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});
});

retrievedData 变量从给定的 POST 路由传递到 GET 路由的最佳方法是什么?

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

作为旁注,res.render() 方法似乎只适用于 app.get() 类型方法

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

推荐答案

您可以使用

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')});
});

但是不能保证返回的数据就是用户设置的数据,只有在用户请求获取路由时才会发送数据.这也使您的 express 服务器有状态,这带来了许多可能的缺点.

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.

您可以在此处

如果您希望您的应用程序是无状态的,您可以改为将您的应用程序传递到外部某个地方,例如独立于 express 服务器的远程服务器,例如 mysql.

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.

如果它有任何用处,如果您愿意,您也可以在同一请求中发送和接收数据,正如在您的评论中,这似乎是您正在尝试执行的操作.如果你有类似下面的东西,它应该可以正常工作,只要你肯定是向/data"发出POST"请求,考虑到它对GET"请求工作正常.

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
  });
});

下面是一个示例 express 应用程序,展示了它是如何工作的

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

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);

如果您启动服务器并转到localhost:8083/form",提交表单后,您会看到一个渲染页面,其中包含您发布的数据.

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.

这篇关于如何在 Express 中的路由之间传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆