Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shows error "Cannot POST" when I try to submit the HTML form

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

like image 415
Abhishek Avatar asked Jan 03 '16 08:01

Abhishek


1 Answers

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.

like image 149
Krzysztof Sztompka Avatar answered Oct 17 '22 12:10

Krzysztof Sztompka