JustMatthew
JustMatthew

Reputation: 372

Why I still get the "Cannot POST" message?

I have a very simple html form, and express server, but I can't make the routing work. I always get "Cannot Post" message. What did I miss?

  var express = require('express');
  var bodyparser = require('body-parser');
  var path = require('path');
  var app = express();

  app.use(express.static("public"));
  app.use(express.bodyParser());

  app.get("/", function(req, res){
  res.sendFile(path.join(__dirname+"/index.html"));
  });

  app.post("/sendform", function(req, res){
  res.send('You sent the name "' + req.query.user + '".');
  });

  app.listen(3000, function(){
  console.log("Server is running at port: 3000");
  }); 


  <form method="post" action="http://localhost:3000/sendform">
    <input type="text" name="username" />
    <input type="submit" value="küldés" />
  </form>

Upvotes: 0

Views: 907

Answers (1)

Yogesh_D
Yogesh_D

Reputation: 18809

With express 4.15.3, you have to use the body parser a bit differently. I changed your code to this and I was able to post to it:

var express = require('express');
var bodyParser = require('body-parser');
var path = require('path');
var app = express();

app.use(express.static("public"));
//app.use(express.bodyParser());
app.use(bodyParser.json({
    limit: "10mb"
}));

app.use(bodyParser.urlencoded({
    limit: "10mb",
    extended: true
}));


app.get("/", function (req, res) {
    res.sendFile(path.join(__dirname + "/index.html"));
});

app.post("/sendform", function (req, res) {
    res.send('You sent the name "' + req.query.user + '".');
});

app.listen(3000, function () {
    console.log("Server is running at port: 3000");
}); 

Upvotes: 1

Related Questions