app.post() not working with Express

怎甘沉沦 提交于 2019-12-05 05:29:28

I don't find any issue with app.post code !!

app.post('/login', function(req, res){
    console.log('test');
    res.end(); // end the response
});

One suggestion is that you should end each response send to client , otherwise your server become unresponsive.

Your post request is send to /test ( since you've used action attribute of your form ), while your express listener is listening to any post requests on /login.

Your code should look like :

block content
  h1 Login
  h3 "tj" and "foobar"
  form(method="post", action="/login")

If you want this to work!

I copy 'router.post()...' to another js file,and change the route in app.js,it worked!But 'router.post()' method still a warning.sometimes it seems like a warning in webstorm,but it can actually work!

Though I haven't gone through the entire code that was mentioned here, I had found a similar issue and was able to debug it.

I'll share where I've gone wrong and maybe that might help someone.

Below code is the part of HTML which went wrong:

<form action="/" method="**post**">

    <select name="crypto">
        <option value="BTC">Bitcoin</option>
        <option value="ETC">Ethereum</option>
        <option value="LTC">Litecoin</option>
    </select>

    <select name="fiat">
        <option value="USD">US Dollors</option>
        <option value="GBP">GB Pounds</option>
        <option value="EUR">EU Euros</option>
    </select>

</form>

Check

Bug in this code is, Though (type) of the is "submit", it has not been included inside tag. Now while you use "app.post" method to get data or trying to "console.log(value)" of the requirement, it can't access.

I was able to debug by including button tag inside the form.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!