如何让我的节点服务器响应多个路径上的POST请求?

时间:2015-05-30 05:15:42

标签: javascript node.js email express nodemailer

我只在一条路径上收到404错误,但成功发送电子邮件,其中包含500条错误响应。

我有两条路径用于处理同一网站上不同表单的电子邮件。只有/send有效但第二个总是失败,除非我也将其路径更改为/send。我正在使用Nodemailer。

Express可以只为每个服务器处理一个POST路径吗?

我会假设与此相关的问题就是问题。

/** * Production Settings */
if (app.get('env') === 'production') {
    // changes it to use the optimized version for production
    app.use('/', express.static(path.join(__dirname, '/dist')));
    // production error handler
    // no stacktraces leaked to user
    app.use(function(err, req, res, next) {
        res.status(err.status || 500);
        res.render('error', {
            message: err.message,
            error: err
        });
    });
}
module.exports = app;

电子邮件相关代码:

var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var nodemailer = require("nodemailer");
var ses = require('nodemailer-ses-transport');
var smtpTransport = require('nodemailer-smtp-transport');
var tableify = require('tableify');
//var routes = require('./routes/index');
//var users = require('./routes/users');
//var mailer = require('express-mailer');
//var mailer = require('./send');
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(logger('dev'));
app.use(cookieParser());

var transporter = nodemailer.createTransport(ses({
    accessKeyId: '****',
    secretAccessKey: '******',
    ServiceUrl: 'email-smtp.us-west-2.amazonaws.com'
}));

/*Contact form mailer*/
app.post('/contactform', function(req, res, next) {
    var sendit = {
        to: '****@gmail.com',
        from: '*****@****nightphoto.com',
        replyTo: req.body.email,
        subject: '*************m' + req.body.name + ' ' + req.body.phone,
        text: req.body.message + '\n' + '\n' + 'Customer Name address: ' + req.body.fname + '\n' + 'Customer email address: ' + req.body.email + '\n' + 'Customer Phone Number: ' + req.body.phone + '\n' + 'Reason:' + req.body.reason,
    };
    transporter.sendMail(sendit, function(req, res, err, data) {
        if (err) {
            console.log(err + "something strange...");
            res.send(err);
        } else {
            res.send("It's ok!");
            console.log("Message sent! ");
        }
        transporter.close();
    })
    res.send(status);
});

//Second path - this works just fine because it's using send.
app.post('/send', function(req, res) {
    var cartContent = req.body.slice(1, 20);
    var content = tableify(cartContent);
    var sendit = {
        to: '***@gmail.com',
        from: '*****l@****nightphoto.com ',
        replyTo: req.body[0].email,
        subject: "*****: " + req.body[0].fname + " " + req.body[0].lname,
        html: '<b>Customer Quote</b>' + '<br>' + 'Customer Name: ' + req.body[0].fname + " " + req.body[0].lname + '<br>' + 'Customer E-mail address: ' + req.body[0].email + '<br>' + 'Customer Phone Number: ' + req.body[0].phone + '<br>' + "Street Address: " + req.body[0].street + "," + req.body[0].state + ',' + req.body[0].zipCode + '<br>' + '<b>Total Price: $</b>' + req.body[0].totalQuote + '<br>' + '<b>Table</b>' + '<br><br>' + content,
    };
    transporter.sendMail(sendit, function(req, res, data, config, status, err) {
        if (err) {
            console.log(err + "something strange..." + data);
        } else {
            console.log(content);
            console.log("Message sent! ");
        }
        transporter.close();
    });
    res.send(status);
});

0 个答案:

没有答案