Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot GET /POST? error in express?

I have a very simple express code

var express = require("express");
var bodyParser = require("body-parser");
var http = require("http");
var path = require("path");

var app = express();
app.use(bodyParser.urlencoded({ extended: false }));

app.set("views",path.resolve(__dirname,"views"));
app.set("view engine","ejs");

app.get("/",function(req,res){
    res.render("index");
});

app.post('/', function(req, res) {
    console.log("posted!");
    res.render("final");
});



http.createServer(app).listen(3000);

Now when I try visiting localhost:3000 everything loads fine, in index.ejs I have a simple form with a input having a name mname, whan i hit a name in the input box and press enter I get This error

Cannot GET /POST?mname=a

I defined a app.post in the code, and asked it to render final.ejs. So where is the code going wrong?

tried other questions saying that express 4 users "router" for routing, tried that but also failed.

like image 384
Bharat Avatar asked Dec 04 '25 13:12

Bharat


2 Answers

You did not include the ejs file with the form you want to submit, but from the error you get it seems you are not doing a POST request, but instead a GET request to path /POST. This is a completely different thing.

I guess in the form you have something like:

 <form action='POST'>

but instead you need something like:

<form action='/' method='POST'>
like image 184
TMG Avatar answered Dec 07 '25 13:12

TMG


I just had a similar problem and I solved it because I forgot to include the app.get for my /post link.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!