gpt4 book ai didi

angularjs - 如何在 Express 中启用 HTML5 模式而不破坏 NodeMailer?

转载 作者:太空宇宙 更新时间:2023-11-04 00:50:38 25 4
gpt4 key购买 nike

大家。我正在制作我的第一个 Node + Express + Angular 应用程序。

我有点用过https://github.com/codeschool/StayingSharpWithAngularSTB作为样板。

总体布局是

[文件夹]应用程序
--------- [子文件夹] 资源(Javascript、CSS、图像等)
--------- [子文件夹] 页面(包含 ng-view 内容)
--------- [子文件夹] View
--------------------index.html(这是将所有内容组合在一起的主要index.html)

[文件夹]node_modules
[文件夹]服务器
--------- [子文件夹] Controller
---------------------- core.server.controller.js
---------expressConfig.js
---------routes.js
应用程序.js
package.json

这是我的服务器配置文件:
应用程序.js

var app = require("./server/routes");

// Start the server
var port = process.env.PORT || 8000;
app.listen(port, function(){
console.log('Listening on port ' + port);
});

/server/expressConfig.js

var bodyParser = require('body-parser');

module.exports = function(app, express) {
// Serve static assets from the app folder. This enables things like javascript
// and stylesheets to be loaded as expected. You would normally use something like
// nginx for this, but this makes for a simpler demo app to just let express do it.
app.use("/", express.static("app/"));

// Set the view directory, this enables us to use the .render method inside routes
app.set('views', __dirname + '/../app/views');

// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));

// parse application/json
app.use(bodyParser.json());
};

/server/routes.js

var express = require('express');
var app = express();
var core = require('./controllers/core.server.controller')
// Load Express Configuration
require('./expressConfig')(app, express);

// Root route
app.get('/', function(req, res){
res.sendfile('index.html', {root: app.settings.views});
});


// routes for sending forms
app.route('/contact-form').post(core.sendMail);
app.route('/table-form').post(core.sendTableRes);
app.route('/artist-form').post(core.sendArtistRes);
module.exports = app;

/server/controllers/core.server.controller.js

var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: //my gmail,
pass: //my gmail password
}
});
// Send an email when the volunteer form is submitted
exports.sendMail = function (req, res){
var data = req.body;
transporter.sendMail({
from: //my gmail,
to: //another gmail,
subject: data.volunteerName+ ' wants to volunteer for our event 2016',
text: 'Volunteer Info \n Name : '+data.volunteerName+'\n Phone Number : '
+data.volunteerNum+'\n E-mail : ' +data.volunteerEmail
});
res.json(data);
};
// Other similar mailing functions

这是发送邮件的 Angular Controller 之一
VolunteerFormController.js

angular.module('MyApp').controller('FormController', function($http){
this.volunteer = {
};
this.addVolunteer = function(){
var data = ({
volunteerName : this.volunteer.name,
volunteerEmail : this.volunteer.email,
volunteerNum : this.volunteer.phone
});

$http.post('/contact-form', data).
then(function(response) {
//show thank you message with animate.css classes and hide form
$(".thanksFriend").addClass("animated tada showBlock");
$("form").addClass("flipOutX animated hideBlock");
}, function(response) {
$(".sorryFriend").addClass("animated tada showBlock");
});

};
});

这效果很好!但是如果我在 Angular 中启用 html5 模式并使用 Express 在 Express 中提供索引

app.use(function(req, res){
res.sendfile('index.html', {root: app.settings.views});
});

在routes.js文件中,Html 5模式效果很好!当我删除井号并刷新时,没有 404s,但是我的联系表单都不起作用,并且控制台没有给我任何错误...我的服务器文件非常小,而且不是 super 复杂,因此应该很容易弄清楚如何同时拥有 HTML5 模式和工作联系表单。有任何想法吗?我对 Express 不太了解,我使用了教程 http://www.bossable.com/1910/angularjs-nodemailer-contact-form/了解如何使用nodemailer。是否有另一种方法来设置 Nodemailer 以使其有效?

我真的很感激一些帮助。这让我快疯了;__;

最佳答案

因此,您必须为每个返回index.html的请求提供服务,

app.get('/', 更改为 app.get('/*',

关于angularjs - 如何在 Express 中启用 HTML5 模式而不破坏 NodeMailer?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32664279/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com