Angular $ http POST从ExpressJS中获得null

我在后台没有经验,并与Node一起工作,尝试设置一个$ http POST请求来发送表单到Express。 每次发出请求时,我的callbackdata都是null 。 也许我的快车路线configuration不正确? 我正在使用Angular与Express / Node进行通信,通过nodemailer(我已经正确configuration)发送一封电子邮件。

这是我的$http POST请求:

客户端/服务/电子邮件/ emailer.js

 angular.module('public').factory('EmailerService',function($http) { return { postEmail: function(emailData, callback) { console.log('call http with object', emailData); $http({ method: 'POST', url: 'http://my-website.com/server/routes/emailer', data: emailData, headers: { "Content-Type": "application/json" }, responseType: 'json' }).success(function(data, status, headers, config) { console.log('success', data, status); }).error(function(data, status, headers, config) { console.log('error', data, status); }).catch(function(error){ console.log('catch', error); }); } }; }); 

这里是我的服务器端快速configuration:

server / routes / emailer.js

 var express = require('express'); var router = express.Router(); var bodyParser = require('body-parser'); var logger = require('morgan'); var app = express(); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: false })); app.use(logger('dev')); app.post('/emailer', function(req,res) { // NOTHING LOGS HERE console.log(res, req, req.body, res.body); }); module.exports = app; 

这里没有logging到控制台, $http请求上的error handling返回:

 emailer.js:4 call http with Object {email: "asdf"} angular.js:8632 OPTIONS http://matt-mcdaniel.com/server/routes/emailer net::ERR_CONNECTION_TIMED_OUT(anonymous function) @ angular.js:8632sendReq @ angular.js:8426$get.serverRequest @ angular.js:8146deferred.promise.then.wrappedCallback @ angular.js:11682deferred.promise.then.wrappedCallback @ angular.js:11682(anonymous function) @ angular.js:11768$get.Scope.$eval @ angular.js:12811$get.Scope.$digest @ angular.js:12623$get.Scope.$apply @ angular.js:12915(anonymous function) @ angular.js:19264jQuery.event.dispatch @ jquery.js:4676jQuery.event.add.elemData.handle @ jquery.js:4360 emailer.js:14 error null 0 emailer.js:16 catch Object {data: null, status: 0, headers: function, config: Object, statusText: ""} 

为了好的措施,因为我是学习Express的新手,所以我会发布我的服务器端app.js

服务器/ app.js

 var express = require('express'); var path = require('path'); var favicon = require('serve-favicon'); var logger = require('morgan'); var cookieParser = require('cookie-parser'); var bodyParser = require('body-parser'); var compression = require('compression'); var routes = require('./routes/index'); var contact = require('./routes/emailer'); var app = express(); // uncomment after placing your favicon in /public //app.use(favicon(__dirname + '/public/favicon.ico')); app.use(logger('dev')); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: false })); app.use(cookieParser()); app.use(compression()); app.use(require('connect-livereload')({ port: 35729, ignore: ['.js'] })); /** Development Settings */ if (app.get('env') === 'development') { // This will change in production since we'll be using the dist folder app.use(express.static(path.join(__dirname, '../client'))); // This covers serving up the index page // app.use(express.static(path.join(__dirname, '../client/.tmp'))); // app.use(express.static(path.join(__dirname, '../client/public'))); // Error Handling app.use(function(err, req, res, next) { res.status(err.status || 500); res.render('error', { message: err.message, error: err }); }); } /** * Production Settings */ if (app.get('env') === 'production') { // changes it to use the optimized version for production app.use(express.static(path.join(__dirname, '/dist'))); // production error handler // no stacktraces leaked to user app.use(function(err, req, res, next) { res.status(err.status || 500); res.render('error', { message: err.message, error: {} }); }); } module.exports = app; app.use(function(req, res) { res.sendfile(__dirname + '/dist/index.html'); }); 

这是我的文件结构:

在这里输入图像说明

你的emailer.js路线肯定有问题。 你应该只有路由逻辑,你不应该重新创build你的快速应用程序。 Express为您提供了一个Router对象,使其变得简单。 例如,你的emailer.js可能是这样的:

 module.exports = express.Router() .post('/emailer', function(req,res) { console.log(res, req, req.body, res.body); res.json({hello:'world'}); }); 

你可以像这样在server/app.js映射这条路线:

 var emailer = require('./routes/emailer'); // ...After all app.use statements, but *before* your error handlers app.use('/server/routes', emailer);