平均堆叠-通过形式从角到表示,以电子邮件发送给Nodemailer

时间:2021-10-28 16:34:06

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.

我对均值堆栈非常陌生,我正在尝试从前端由角JS处理的内容表单发送值,然后传递给后端Express,由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.

我在从角度到表示的路由上遇到了一些问题,对于这个堆栈,文档似乎是相当稀少的。我还假设我将使用node mailer发送电子邮件,任何关于它的信息或最佳实践也将受到赞赏。

My app.js

我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

我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'

我看到的错误在联系人。对象#没有“post”方法

Here is my folder structure: 平均堆叠-通过形式从角到表示,以电子邮件发送给Nodemailer

以下是我的文件夹结构:

2 个解决方案

#1


2  

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

虽然之前的回答被接受了,但我还是想说明一下错误的原因。

Your contact.js should've looked like

你的联系。js应该看起来像

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.

错误的原因正在联系中。导出中没有定义post方法。所以你不能叫它。您需要的是上面的代码。

#2


2  

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.

我建议你调查一下这样的事情。在express端实现REST之后,使用AngularJs端上的restangle使用REST API非常简单。

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.

这种转换的原因非常简单,现在实现代码的级别太低了。将它移动到更高的级别可以让您避免重新实现已经处于restangle的东西。

#1


2  

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

虽然之前的回答被接受了,但我还是想说明一下错误的原因。

Your contact.js should've looked like

你的联系。js应该看起来像

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.

错误的原因正在联系中。导出中没有定义post方法。所以你不能叫它。您需要的是上面的代码。

#2


2  

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.

我建议你调查一下这样的事情。在express端实现REST之后,使用AngularJs端上的restangle使用REST API非常简单。

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.

这种转换的原因非常简单,现在实现代码的级别太低了。将它移动到更高的级别可以让您避免重新实现已经处于restangle的东西。