I'm doing an app with node.js for server side and angular for frontend part. I'm having a problem with a simple post method, the problem is when I send my json to server, it is taking it like a json key. I'm using body-parser, maybe the problem is with that? This is my code:
Angular:
var request = $http({
method: "POST",
url: 'http://localhost:3000/register',
data: {
"username": "filip"
},
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
});
node.js
var express = require("express");
var bodyParser = require("body-parser");
var morgan = require("morgan");
var app = express();
var config = require("./config");
var mongodb = require("mongodb")
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.use(morgan("dev"));
app.post("/register", function(req, res, next) {
res.setHeader('Access-Control-Allow-Origin', 'http://localhost:8080');
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader('Access-Control-Allow-Credentials', true);
console.log(req.body);
});
This is what i get on the server like a request:
{ '{"username":"filip"}': '' }
I'm just started to learn node.js and i'm stuck with this simple thing, anybody help?