我正在尝试将AngularJS的POST数据发送到Express。我现在正在做的是尝试通过卷曲发送一些POST内容。
我已经尝试过发送数据了。它被发送,我得到200作为回应。但每当我尝试通过req.body通过body-parser访问数据时,我会得到一个500,并且我在最底部发布了错误。
我对此事进行了大量研究(过去4-5小时......),我认为我检查了以下常见问题:
卷曲:
curl -X POST -H "Content-Type: application/json" -d '{"username": "gala", "email": "ga@ga.g", "password": "123"}' localhost:3000/users
express.js
// glowny plik przygotowujacy aplikacje express
// wczytanie zaleznosci
var express = require('express')
var stylus = require('stylus')
var parser = require('body-parser')
var morgan = require('morgan')
var compress = require('compression')
var methodOverride = require('method-override')
// przygotowanie aplikacji, obsluga middleware i na koncu zwrocenie obiektu aplikacji
module.exports = function() {
var app = express()
if (process.env.NODE_ENV === 'development') {
app.use(morgan('dev'))
} else if (process.env.NODE_ENV === 'production') {
app.use(compress())
}
app.use(parser.urlencoded({ extended: true }))
app.use(parser.json())
app.use(methodOverride())
// uzycie jade jako silnika szablonow dla html
app.set('views', __dirroot + '/app/views')
app.set('view engine', 'jade')
require(__dirroot + '/app/routes/index.server.route.js')(app)
require(__dirroot + '/app/routes/users.server.route.js')(app)
// mozliwosci uzywania statycznych plikow z folderu /public
app.use(express.static(__dirroot + '/public'))
return app
}
users.server.route.js:
var users = require(__dirroot + '/app/controllers/users.server.controller.js')
module.exports = function(app) {
app.route('/users').post(function() {
users.create()
})
}
users.server.controller.js:
var User = require('mongoose').model('User')
exports.create = function(req, res, next) {
console.log(req.body)
}
错误讯息:
TypeError: Cannot read property 'body' of undefined
at Object.exports.create (/home/gala/Projekty/cantr-crafting/app/controllers/users.server.controller.js:4:20)
at /home/gala/Projekty/cantr-crafting/app/routes/users.server.route.js:5:15
at Layer.handle [as handle_request] (/home/gala/Projekty/cantr-crafting/node_modules/express/lib/router/layer.js:82:5)
at next (/home/gala/Projekty/cantr-crafting/node_modules/express/lib/router/route.js:110:13)
at Route.dispatch (/home/gala/Projekty/cantr-crafting/node_modules/express/lib/router/route.js:91:3)
at Layer.handle [as handle_request] (/home/gala/Projekty/cantr-crafting/node_modules/express/lib/router/layer.js:82:5)
at /home/gala/Projekty/cantr-crafting/node_modules/express/lib/router/index.js:267:22
at Function.proto.process_params (/home/gala/Projekty/cantr-crafting/node_modules/express/lib/router/index.js:321:12)
at next (/home/gala/Projekty/cantr-crafting/node_modules/express/lib/router/index.js:261:10)
at methodOverride (/home/gala/Projekty/cantr-crafting/node_modules/method-override/index.js:77:5)
答案 0 :(得分:4)
我不使用Node或Express,但至少不应该将req
从路由处理程序传递到users.create()
,即
app.route('/users').post(function(req, res) {
users.create(req, res)
})
或者甚至
app.route('/users').post(users.create);
答案 1 :(得分:1)
TypeError:无法读取属性' body' POST(MEAN)后未定义的 当你的localhost ip被更改或请求的ip不正确时,你会在post url中提到这个错误。 我在排除以太网故障时遇到了同样的错误。然后我的IP地址被更改了。所以检查你的IP。它会工作..
答案 2 :(得分:0)
发生这种情况是因为我在chrome控制台上添加了一些测试脚本,并且似乎存在干扰。您可以手动清理控制台,也可以在其他实例Chrome中运行您的应用。
答案 3 :(得分:0)
const express = require("express");
const bodyParser = require("body-parser");
const app = express();
app.use(bodyParser.urlencoded({extended: true}));
app.get("/",function(req,res){
res.sendFile(__dirname + "/index.html");
});
//Post processing after taking data from user..
app.post("/",function(req,res,next){
var num1 = Number(req.body.num1);
var num2 = Number(req.body.num2);
var result = num1+num2;
res.send("The result is "+ result);
});
// To create a server...
app.listen(3000, function(){
console.log("Server started..3000");
});
我希望这段代码也对您有用。您可以从上方寻求帮助。