I am pretty new to the MEAN stack and am trying to send the values from a content form that is processed by Angular JS on the front-end and then passed to Express on the backend to be sent as an email by Nodemailer.

I am having issues with the routing from Angular to Express and the documentation seems to be pretty sparse for this stack. Also I am assuming I would use node mailer to send the email, any info or best practices on that would also be appreciated.

My app.js

var express = require('express');
var routes = require('./routes');

var user = require('./routes/user');
var contact = require('./routes/contact');  // Contact Form
var http = require('http');
var path = require('path');

var app = express();
var nodemailer = require("nodemailer");

// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));

// development only
if ('development' == app.get('env')) {
  app.use(express.errorHandler());
}

app.get('/', routes.index);
app.get('/users', user.list);
app.post('/contact', contact.process);  // Contact form route


http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});

My contact.js

exports.post('/contact', function(req, res) {
console.log('I Made it to Express');
process(req, res);
});

My Angular Controller

app.controller('contactController', function($scope, $http, $location) {
$scope.formData = {};

$scope.processForm = function() {
    console.log('Im in the controller');

    // Trigger validation flag.
    $scope.submitted = true;

    $http.post('/contact', {
        name: $scope.formData.name,
        email: $scope.formData.email,
        message: $scope.formData.message
    }).success(function(data, status, headers, config) {
            if(data.success){
                $location.path('/');
            }else {
                //do something about the error
            }
        });
};

});

And finally my Angular app

'use strict';
var app = angular.module('app', ['ngRoute']).config(function($routeProvider, $locationProvider) {


// Route: Default Widget View
$routeProvider.when('/contact', {
    controller: 'contactController'
});


});

The error I am seeing is in the contact.js - TypeError: Object # has no method 'post'

Here is my folder structure: enter image description here

有帮助吗?

解决方案

Though the previous answer is accepted, I'd like to address the reason for the error.

Your contact.js should've looked like

function process(req, res) {
  //handle request here
}
exports.process = process;

Reason for the error is in contact.js you dont have post method defined in exports. So you cannot call it. what you want is the above code.

其他提示

I recommend you to look into something like this. After you implement REST on express side, it's really simple to work with REST API using Restangular on AngularJs side.

Reasons for that transition are really simple, right now you are implementing your code too low level. Moving it on higher level allows you to escape reimplementing things that already in Restangular.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top