1

I am having a strange problem with express for Node.js. It is unable to get post requests from the client. The get request works, but for some reason the post request is not working.

Here is some simple code below that gives me an error:

var express = require('express');
var app = express();

app.post('/', function(req, res){
    res.end('Express post page!');
});

app.listen(4001);

When I try and visit the page in the browser it says: Cannot GET /

Again when I use a get request it works fine, but when I try and use a post request it gives me this error. Any help would be appreciated.

P.S. I am using the latest version of Express 4.4.5

3
  • Are you just loading the page in your browser or somehow making an actual POST request. Since you have no GET route set for /, the error Cannot GET / suggests that you're trying to do a GET request, and not a POST. Commented Jul 1, 2014 at 4:18
  • you should say it app.get, basically when you hit someurl from browser it will be a GET request, and you have defined as POST. POST is useful when you want to send some data after form submitting. Commented Jul 1, 2014 at 4:19
  • Ok, thanks. That makes a lot of sense. However I put a console log in the post request it never displays and whenever i try to post through my app it gives me an error. (i am not trying to load a url) Commented Jul 1, 2014 at 5:28

4 Answers 4

3

what you tried was not a post request...the default type of request type is GET for the browser.. so add the code in you app:

app.get('/', function(req, res){
    res.end('Express get page!');
});
Sign up to request clarification or add additional context in comments.

Comments

1

As commenters have said, it sounds like you're trying to navigate to the URL via the browser URL which is always a GET. Try installing Postman on your Chrome browser and using it to test your API with a POST.

Comments

0

Go TO Chrome App's search for Rest Client and try using its POST

By default your browsers can't post it independently it needs an app to do POST/PUT/DELETE. That's the reason its searching for GET verb

Comments

0

By default you browser just do GET for you, not POST. So try to POST via POSTMAN. Hope it'll help you.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.