简体   繁体   English

为什么发布请求在本地而不是在远程工作? 搜索的网址不同

[英]Why does post request work locally and not remotely? url searched for is different

I have a page with buttons at a specific url. 我有一个在特定网址上带有按钮的页面。 here they are locally and remotly 在这里,他们在本地和远程

local : http://localhost:4000/individual/comp6/57d614b711d268030080eda9/ 本地: http://localhost:4000/individual/comp6/57d614b711d268030080eda9/

remote : https://linknon.herokuapp.com/individual/comp6/57d614b711d268030080eda9/ (THIS IS NOT MY REAL LINK) 远程: https://linknon.herokuapp.com/individual/comp6/57d614b711d268030080eda9/ : https://linknon.herokuapp.com/individual/comp6/57d614b711d268030080eda9/ (这不是我的真实链接)

when I click on the button and have app.post("/individual/"+ comp + "/usefulness", postHandler) in my code, localhost works (the db get updated) 当我单击按钮并在我的代码中包含app.post("/individual/"+ comp + "/usefulness", postHandler)时,localhost有效(数据库得到更新)

but when I go on line and click on the button I get Failed to load resource: the server responded with a status of 404 (Not Found) it could not get https://linknon.herokuapp.com/individual/comp6/57d614b711d268030080eda9/usefulness .I found that to be weird . 但是当我上线并单击按钮时,我Failed to load resource: the server responded with a status of 404 (Not Found)它无法获得https://linknon.herokuapp.com/individual/comp6/57d614b711d268030080eda9/usefulness 。我发现这很奇怪。 Why did it work on local and not remote ? 为什么在本地而不是远程工作

my app.post("/individual/"+ comp + "/usefulness", postHandler) doesnt include the id after the company name (the company name is the comp var). 我的app.post("/individual/"+ comp + "/usefulness", postHandler)在公司名称(公司名称为comp var)之后不包含ID。 I'm assuming that for remote I had to add in Id for some reason. 我假设对于远程,由于某种原因我必须添加ID。 I tried something like using regular expression (never did that b4) app.post("/individual/.+/usefulness", postHandler) did not work on local and wondering if it would work on remote. 我尝试过使用正则表达式之类的东西(b4从未这样做过) app.post("/individual/.+/usefulness", postHandler)在本地不起作用,想知道它是否在远程也可以工作。

I also do post requests in the same module like this 我也像这样在同一模块中发布请求

app.post("/usefulness", postHandler);
app.post("/reviewfor/usefulness", postHandler);

those seem to work 那些似乎有效

I feel like they all have in common usefulness at the end so maybe a regular expression would work. 我觉得它们最后都有共同的usefulness ,因此也许可以使用正则表达式。

This is your problem. 这是你的问题。 Don't use variables this way when defining routes: 定义路由时,请勿以这种方式使用变量:

app.post("/individual/"+ comp + "/usefulness", postHandler)

It mixes up app-load-time variables ( comp in this case) with what should be per-request variables. 它将应用程序加载时间变量(在这种情况下为comp )与应请求变量混合在一起。 Express handles per-request variables with route parameters. Express使用路由参数处理每个请求的变量。 So you need something like: 因此,您需要类似:

app.post("/individual/:comp/usefulness", postHandler)

Then in the body of postHandler you can access req.params.comp which will be the specific value from the URL on this request like comp1 , comp2 , comp3 etc. 然后在postHandler的正文中,您可以访问req.params.comp ,它将是此请求上的URL中的特定值,例如comp1comp2comp3等。

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

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