Shows a "Can not POST" error when I try to submit an HTML form

I saw other questions with the same error, but none of the answers work.

<!DOCTYPE html> <html> <body> <form action="http://127.0.0.1:8080/del" method="post"> First name: <input type="text" name="fname"><br> Last name: <input type="text" name="lname"><br> <input type="submit" value="Submit"> </form> <p>Click on the submit button, and the input will be sent to a page on the server called "http://127.0.0.1:8080/del".</p> </body> </html> 

server.js

 var express=require('express'); var body_parser=require('body-parser'); var request = require('request').defaults({json:true}); var app=express(); var del=require('./del'); app.post('./del',del.test); var server = app.listen(8080,function(){ var host="127.0.0.1"; var port="8080"; console.log("App is listening at http://%s:%s\n",host,port); }); 

del.js

 module.exports={ test: function(){ console.log("Hello world."); } }; 

Every time I submit a form, it shows

Cannot POST / del

+5
source share
1 answer

In your server.js, change this line:

 app.post('./del',del.test); 

:

 app.post('/del',del.test); 

then you have the correct router.

And your del.js will change to this:

 module.exports={ test: function(req, res){ console.log("Hello world."); res.status(200).end(); } }; 

since the router function should return a response.

+1
source

Source: https://habr.com/ru/post/1239708/


All Articles