jack blank jack blank - 2 years ago 92
HTTP Question

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/


remote :
https://linknon.herokuapp.com/individual/comp6/57d614b711d268030080eda9/
(THIS IS NOT MY REAL LINK)

when I click on the button and have
app.post("/individual/"+ comp + "/usefulness", postHandler)
in my code, localhost works (the db get updated)

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 . 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). I'm assuming that for remote I had to add in Id for some reason. 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.

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.

Answer Source

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. Express handles per-request variables with route parameters. 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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download