angularjs - Angular $http POST gets null from ExpressJS -
i'm inexperienced in backend , working node , trying set $http post request send form express. each time make request, callback data
null
. perhaps express routes not configured correctly? i'm using angular communicate express/node send email through nodemailer (which i've configured).
here $http
post request:
client/service/emailer/emailer.js
angular.module('public').factory('emailerservice',function($http) { return { postemail: function(emaildata, callback) { console.log('call http 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); }); } }; });
here server side express 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;
nothing logs console here, , error handling on $http
request returns this:
emailer.js:4 call http 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: ""}
for measure, , since i'm new learning express, i'll post server side app.js
.
server/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 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') { // change in production since we'll using dist folder app.use(express.static(path.join(__dirname, '../client'))); // covers serving 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 use optimized version production app.use(express.static(path.join(__dirname, '/dist'))); // production error handler // no stacktraces leaked 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'); });
here file structure:
you've got problems in emailer.js
route. should have routing logic there, shouldn't recreating express app. express gives router
object make easy. example, emailer.js
this:
module.exports = express.router() .post('/emailer', function(req,res) { console.log(res, req, req.body, res.body); res.json({hello:'world'}); });
and can map route in server/app.js
so:
var emailer = require('./routes/emailer'); // ...after app.use statements, *before* error handlers app.use('/server/routes', emailer);
Comments
Post a Comment