问题
Hi all I am trying to send email from my website with nodemailer and express routing after press send button i have info /contactForm 500 internal server error any idea how to solve it.
//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 - express routing
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);
});
}
}
})();
回答1:
I think you should parse
the body
to JSON. Try the following:
app.use(bodyParser.json());
I think you received a JS Object
instead of JSON
from the client.
Here is more info on how to solve your problem.
来源:https://stackoverflow.com/questions/43845114/node-js-nodemailer-post-internal-server-error-500