大家好我正试图从我的网站发送电子邮件与nodemailer和快速发送按钮后的快递路由我有info / contactForm 500内部服务器错误任何想法如何解决它。
// app.js
// CONFIG
const express = require('express');
const app = express();
const exphbs = require('express-handlebars');
const compression = require('compression');
const config = require('./src/server/config/config');
const http = require('http');
const path = require('path');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const routes = require('./src/server/routes/index');
const nodemailer = require('./src/server/email/emailTransporter');
// view engine setup
const hbs = exphbs.create({
defaultLayout: 'layout',
extname: '.hbs',
partialsDir: [
'./src/client/views/'
]
});
app.set('views', path.join(__dirname, './src/client/views'));
app.engine('handlebars', hbs.engine);
app.set('view engine', 'handlebars');
/**
* Get port from environment and store in Express.
*/
const port = normalizePort(process.env.PORT || '3000');
const environment = process.env.NODE_ENV;
app.use(compression());
app.set('port', port);
app.use('/', express.static(__dirname, './src/client/views', {
maxage: '1y'
}));
app.use('/', routes);
app.use('/portfolio', routes);
/**
* Create HTTP server.
*/
const server = http.createServer(app);
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
/**
* Normalize a port into a number, string, or false.
*/
function normalizePort(val) {
const port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
}
/**
* Event listener for HTTP server "error" event.
*/
function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
const bind = typeof port === 'string' ? 'Pipe ' + port : 'Port ' + port;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
// index.js - 快速路由
const express = require('express');
const router = express.Router();
const config = require('../config/config');
const sendEmail = require('../email/emailTransporter');
const nodemailer = require('nodemailer');
/* Set HEaders. */
router.use('/*', function(req, res, next) {
res.setHeader('Cache-Control', 'public, max-age=864000');
res.setHeader("Content-Type", "text/html");
next();
});
/* GET home page. */
router.get('/', function(req, res, next) {
var vm = {
title: 'Portfolio'
};
res.render('index.hbs', vm);
});
/* GET portfolio. */
router.get('/portfolio', function(req, res, next) {
var vm = {
title: 'portfolioPage'
};
res.render('portfolio.hbs', vm);
});
/* SEND EMAIL . */
router.post('/contactForm', sendEmail);
module.exports = router;
// angular Contact Controller
(function() {
'use strict';
angular
.module('app.layout')
.controller('contactCtrl', contactCtrl);
/* @ngInject */
function contactCtrl(emailService) {
var vm = this;
// CONTACT FORM
vm.data = {};
vm.invalidClass = false;
vm.sendEmail = function(contactForm) {
vm.submitClicked = true;
if (contactForm.$valid) {
emailService.sendEmail(vm.data);
} else {
vm.invalidClass = true;
}
};
}
})();
// Angular Email Services
(function() {
'use strict';
angular
.module('app.layout')
.factory('emailService', emailService);
/* @ngInject */
function emailService($http, growl) {
var service = {
sendEmail: sendEmail
};
return service;
function sendEmail(emailData) {
return $http.post('/contactForm', emailData)
.then(function(data) {
growl.addSuccessMessage("Your Email was sent");
})
.catch(function(err) {
growl.addErrorMessage("Message not sent");
console.log(err);
});
}
}
})();
答案 0 :(得分:2)
我认为你应该解析json使用app.use(bodyParser.json());我以为你收到的对象而不是来自客户端的jspon这里有更多信息https://www.gitbook.com/book/kevinchisholm/handling-post-requests-with-express-and-node-js/details如何解决你的问题