快速获取请求为空

时间:2018-02-19 23:51:11

标签: javascript node.js angular express http-headers

我会将数据从角度发送到Express。

在我的服务中;

sendtext(text): Observable<any> {
  console.log(text);
  return this.http.post<string>(this.url, text, httpOptions);
}

我配置标题

const httpOptions = {
  headers: new HttpHeaders({
    'Content-Type': 'application/x-www-form-urlencoded'
  })

我的console.log(text)打印输入中键入的正确文本。

在我的服务器端,我创建了我的server.js文件

var http = require("http");
var admin = require('firebase-admin');
var firebase = require("firebase");
var express = require("express");
var app = express();
var routerProj = require("./routes/routes");
var bodyParser = require("body-parser");
var port = process.env.app_port || 8080; // set our port
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
var server = app.listen(port);

app.use(function (req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header('Access-Control-Allow-Methods', 'GET, POST, PUT ,DELETE');

  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept,*");
  next();
});
var config = {
   ......configuration...
};
firebase.initializeApp(config);


var serviceAccount = require("./ServiceAcountKey.json");
admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "https://datatable-18f93.firebaseio.com"
});
app.use("/v1", routerProj);
//Create HTTP server and listen on port 8000 for requests

// Print URL for accessing server
console.log("Server running at http://127.0.0.1:8080/");

我创建了路由器文件:

var express = require("express"); // call express
var router = express.Router(); // get an instance of the express Router
var admin = require("firebase-admin");
router
  .route("/")
  .post(function (req, res, err) {
    console.log(req.body);
    // Get a database reference to our posts
    var db = admin.database();
    var ref = db.ref("/");

    // Attach an asynchronous callback to read the data at our posts reference
    ref.push(
      {
        "text": req.body
      }

    );

  });
module.exports = router;

这里console.log(req.body)打印一个空对象{}

是不是因为标题?如果是,我该如何配置它们?我已经尝试了'Content-Type': 'application/json',没有标题,没有结果。

0 个答案:

没有答案