我对 MEAN 堆栈非常陌生,我正在尝试从前端由 Angular JS 处理的内容表单发送值,然后在后端传递给 Express 以由 Nodemailer 作为电子邮件发送。
我在从 Angular 到 Express 的路由方面遇到问题,并且该堆栈的文档似乎非常稀疏。此外,我假设我将使用节点邮件程序发送电子邮件,任何信息或最佳实践也将不胜感激。
我的 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'));
});
我的contact.js
exports.post('/contact', function(req, res) {
console.log('I Made it to Express');
process(req, res);
});
我的角度控制器
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
}
});
};
});
最后是我的 Angular 应用
'use strict';
var app = angular.module('app', ['ngRoute']).config(function($routeProvider, $locationProvider) {
// Route: Default Widget View
$routeProvider.when('/contact', {
controller: 'contactController'
});
});
我看到的错误在contact.js - TypeError: Object # has no method 'post'
这是我的文件夹结构: