简体   繁体   English

如何在节点js中的另一个js文件中引用发布请求值?

[英]How to reference a post request value in another js file in node js?

Long time, first time. 好久不见 I am new to learning node JS, Javascript, express, etc. and have been dealing with a problem for three days and counting. 我是学习节点JS,Javascript,Express等的新手,并且已经处理了三天的问题并开始计算。 Hoping for some kind help here. 希望在这里有所帮助。

I have a view (called 'standings' view) where the user fills in an HTML form entering their username and bunch of team names. 我有一个视图(称为“站立”视图),在该视图中,用户填写了HTML表单,输入了用户名和一组团队名称。

  • I handle the post request using express and save the data in mongodb 我使用express处理发布请求并将数据保存在mongodb中
  • The request contains a username field which can be referenced by 'req.body.username' inside the express app.post method. 该请求包含一个用户名字段,该字段可以由express app.post方法中的'req.body.username'引用。

What I am trying to do is capture this 'req.body.username' from the POST request and use it in another JS file (called 'thankyou.js') which would be the config file for a "thankyou" view. 我想做的是从POST请求中捕获此“ req.body.username”,并在另一个JS文件(称为“ thankyou.js”)中使用它,该文件将成为“ thankyou”视图的配置文件。 In other words, I am looking for a way to somehow reference the 'req.body.username' parameter directly without having to go back to the DB and in my "thankyou" view I want to say something like: 换句话说,我正在寻找一种方法,以某种方式直接引用'req.body.username'参数,而不必返回数据库,在我的“ thankyou”视图中,我想说些类似的东西:

"Thank you" + <%= req.body.username %> + "for contacting us".

What would be the best approach to accomplish this? 什么是实现这一目标的最佳方法? I have thought of and researched the following but not quite sure how to implement: 我已经考虑并研究了以下内容,但不确定如何实现:

  • Somehow export 'req.body.username' property in my standings JS file and require/reference it in the thankyou JS file. 以某种方式在我的状态JS文件中导出“ req.body.username”属性,并在thankyou JS文件中要求/引用它。
  • Using query strings to pass the 'req.body.username' in standings.js to the thankyou view. 使用查询字符串将standings.js中的“ req.body.username”传递到thankyou视图。

Here is my express app.post method which handles the POST call from the user for the 'standings' view: 这是我的express app.post方法,用于处理用户对“站立”视图的POST调用:

app.post('/standings', urlencodedParser, function(req, res){

var newStandings = standingsModel.update({ username: req.body.username}, req.body, {upsert: true}, function(err,data){
  if (err) throw err;

res.json(data);
})
});

Thanks in advance. 提前致谢。

Are these REST calls? 这些是REST调用吗? It looks like it, so I'm going to assume that this is stateless and you can't use session, right? 看起来像这样,所以我将假定这是无状态的,您不能使用会话,对吗?

When do you send the user to the "thank you" page? 您何时将用户发送到“谢谢”页面? If it's after submission, can you do a redirect instead of making everything an AJAX call? 如果是在提交之后,您可以进行重定向,而不是将所有内容都进行AJAX调用吗?

I would take a look at this answer on passing data between routes/pages in ExpressJS. 我将在ExpressJS中的路由/页面之间传递数据时看一下这个答案 See if that offers any value to you. 看看这是否对您有任何价值。 You might have to make some architectural changes. 您可能需要进行一些体系结构更改。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM