javascript - how to make post request in node js + express -
i trying 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
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 application after database connection 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 hit browser http://localhost:3000/api/users nothing happen ..
i try chrome rest client sent post request .. in screen shots .but did not receive console message or response server doing wrong..
i getting message console listening on port 3000
the first picture shows get
request, since browser defaults get
. since don't allow get
generic error.
with rest-client need set headers content-type: application/json
. should work.
Comments
Post a Comment