javascript - 如何在 Express/Node.js 中反序列化 JSON 数据

标签 javascript node.js angularjs express

所以我在客户端有一个 Controller :

 $scope.authenticate = function() {
            var creds = JSON.stringify({email: this.email, password: this.password});
            $http.post('/authenticate', creds).
                success(function(data, status, headers, config) {
                   // etc
                }).
                error(function(data, status, headers, config) {
                  // etc
                });
        };

在服务器端:

app.post('/authenticate', function(req, res) {
    console.log("Unserialized request: " + JSON.parse(req));
});

但是当我尝试解析请求时出现错误。我不知道为什么。有什么想法吗?

最佳答案

使用 express.bodyParser 中间件,它将为您进行解析,并为您提供 req.body 作为准备就绪的对象。

var express = require('express');

app.post('/authenticate', express.bodyParser(), function(req, res) {
    console.log("Unserialized request: " + req.body);
});

关于javascript - 如何在 Express/Node.js 中反序列化 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19967851/

相关文章:

javascript - 如何使用集群处理 Socket.IO 房间?

javascript - 在Python中使用for循环编写多个html文件

node.js - 从 Nestjs 中可观察的 grpc 服务中获取数据,

node.js - 当前写入文件时如何处理 readFile

node.js - Mongoose 架构类型字符串不起作用

c# - Entity Framework 在 db.SaveChanges() 期间创建新数据行

javascript - 将 tastypie 过滤器与 AngularJS 和 RESTANGAL 一起使用

javascript - Angularjs $http 不发送表单数据到服务器

javascript - JSON 对象的已知非空值的字段返回空字符串

angularjs - 由 : java. lang.IllegalStateException : required key [datasource. Sampleapp.hibernate.dialect] 未找到引起