I can not get post params on the server. I am sending a request to send to an Angular 2 application to a Nodejs express server. Here is my code in Angular 2:
import { Injectable } from 'angular2/core'; import { Http } from 'angular2/http'; @Injectable() export class QueryService { static get parameters() { return [[Http]] } constructor(http) { this.http = http; } postApi() { var headers = new Headers(); headers.append('Content-Type', 'application/json'); return this.http.post('http://localhost:3001/post_test', JSON.stringify({"id": 1, "name": "2"}), { headers: headers }).toPromise(); } }
In the browser, I see that post params were sent, for example, the chrome "Request Playload" section contains my data. And here is my server:
app.js:
var bodyParser = require('body-parser'); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({extended: true}));
routes / index.js:
exports.post_test = function(req, res) { console.log('post_test ', req.body); }
and the output is post_test {} "
I can not understand where the problem is. Because my server works fine when I used the Angular 1 $ http service for mail requests. Please help me!
source share