I am trying to make post request using express + node.js .I install these plugins package.json
{
"name": "expressjs",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.14.1",
"express": "^4.13.3",
"mongodb": "^2.0.47"
}
}
I make post request like that
var express = require('express');
var mongodb = require('mongodb');
var app = express();
var bodyParser = require('body-parser');
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
var MongoClient = require('mongodb').MongoClient;
var db;
// Initialize connection once
MongoClient.connect("mongodb://localhost:27017/abc", function(err, database) {
if(err) throw err;
db = database;
// Start the application after the database connection is ready
app.listen(3000);
console.log("Listening on port 3000");
});
app.post('/api/users', function(req, res) {
console.log('---')
var user_id = req.body.id;
var token = req.body.token;
var geo = req.body.geo;
res.send(user_id + ' ' + token + ' ' + geo);
});
When I hit browser http://localhost:3000/api/users like that nothing happen ..
I also try chrome rest client to sent post request .. I do like that in screen shots .But I did not receive any console message or response from server where i am doing wrong..
I am getting only this message from console Listening on port 3000
The first picture shows a GET
request, since the browser defaults to GET
. Since you don't allow for a GET
you just a generic error.
With the ReST-client you need to set headers to Content-Type: application/json
. This then should work.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.