简体   繁体   English

如何在 Express 中将价值从一条路线传递到另一条路线?

[英]How to pass value from one route to another in Express?

I have a simple route that makes a POST request and returns an object with a number of values.我有一个简单的路由,它发出一个 POST 请求并返回一个具有多个值的对象。 I want to use one of the returned values in another (different) route for different logic.我想将另一个(不同)路由中的返回值之一用于不同的逻辑。 Is there a way to pass such data from one route to another?有没有办法将这些数据从一条路线传递到另一条路线?

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

    // call some function that returns an object for use in another route eg:
    myFunction()
    .then((data) => {
        console.log(data)
    })
    .catch((err) => {
        console.log(err)
    })
});

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

    // use the data returned from the register route for verification logic 

});



You can use npm package connect-flash for passing data between routes您可以使用 npm package connect-flash在路由之间传递数据

var express = require('express');
var flash = require('connect-flash');
var app = express();
app.use(flash());

app.get('/login', function(req, res){
  // Set a flash message by passing the key, followed by the value, to req.flash().
  req.flash('username', 'Gaurav Gupta')
  res.redirect('/profile');
});

app.get('/profile', function(req, res){
  // Get an array of flash messages by passing the key to req.flash()
  let message = req.flash('username')
  res.render('index', { message: message }); // or {message} only es6 feature
});

Or alternatively you can use middlewares on the routes或者,您可以在路由上使用中间件

ie IE

router.post('/register', middleware(), (req, res) => {
   ...
});

//define middleware //定义中间件

function middleware(){
  return function(req, res, next){
    ...perform actions
    next()
  }
}

//reuse middleware in route //在路由中重用中间件

router.post('/verify', middleware(), (req, res) => {
   ...
});

You can try express-session to store the value in the session then read it from session variable.您可以尝试 express-session 将值存储在会话中,然后从会话变量中读取它。 or you can create an encrypted token and send it to the user as register response, then in verify the route take the token and decrypt it.或者您可以创建一个加密的令牌并将其作为注册响应发送给用户,然后在验证路由时获取令牌并对其进行解密。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在express-js中将变量从一个路由传递到另一个路由 - How to pass a variable from one route to another in express-js Node.js将值从一条路由传递到另一条路由 - Node.js pass value from one route to another route (Express)如何将一个值从 function 传递给 express.Router().get 中的另一个值 - (Express)How to pass a value from function to another one in express.Router().get 如何在 Express 中将请求正文的数据从一条路由移动到另一条路由 - How to move data of request body from one route to another in Express 在 Express 中从一条路线重定向到另一条路线 - Redirect from one route to another in Express 如何使用expressjs将数据从一个路由传递到另一个nodejs - How to pass data from one route to another nodejs with expressjs 如何将值从一个 function 传递到另一个 - How to pass a value from one function to another 如何将 state 从一条反应路线传递到另一条反应路线? - How can i pass a state from one react route to another react route? Express-如何从一个表单传递一个值并从另一个 html 接收它并访问 js 中的值? - Express-How do I pass a value from a form and receive it from another html and access the value in js? 如何在React中将单击的组件中的数据从一条路径传递到另一条路径? - How do I pass data from a clicked component from one Route to another in React?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM