我已經從一個AngularJS控制器打印數據到工廠,所以客戶端有API數據,但是當我發佈數據到服務器端時,它打印的是undefined req.body
。任何想法下面的代碼中缺少什麼?如何使用nodejs創建POST rest api?
我已經加入body-parser
模塊app.js
angularJsFactory.js
angular.module('App').factory('LogsFactory', function($http) {
'use strict';
return {
addNewUser: function(user) {
console.log('user in factory', user);
return $http.post('/newUser', user);
}
}
});
節點App.js
var express = require('express');
var app = express();
var fs = require('fs');
var path = require('path');
var jsonfile = require('jsonfile');
var async = require('async');
var server = require('http').createServer(app);
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser')
var io = require('./app/io').initialize(server);
app.use(cookieParser());
require('./app/serverfiles/components/auth')(app);
app.use(express.static(__dirname + "/public"));
app.post('/newUser',function(req,res){
console.log(req.body);
});
什麼是印刷'的console.log( '用戶在工廠',用戶);'? – lealceldeiro
您使用的是Express嗎? – Aron
@lealceldeiro工廠用戶{「firstName」:「John」,「lastName」:「Strat」,「email」:「[email protected]」,「_ id」:「id8976」,「type」:「user」 ,「attuid」:「kj897s」,「message」:「用戶需要訪問」} – hussain