Shows error "Cannot POST" when I try to submit the HTML form
22,631
In your server.js change this line:
app.post('./del',del.test);
to this:
app.post('/del',del.test);
then you have correct router.
And your del.js change to this:
module.exports={
test: function(req, res){
console.log("Hello world.");
res.status(200).end();
}
};
because router function should return response.
Author by
Abhishek
Updated on August 19, 2022Comments
-
Abhishek over 1 year
I have seen other questions with the same error, but none of the answers seem to 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."); } };
Each time, when I submit the form, it shows
Cannot POST /del