联系我们表格Node Js / express 404错误

时间:2018-04-05 20:25:24

标签: html node.js forms express

我正在尝试创建一个允许某人向我发送电子邮件的表单。 但是,我收到404错误,如下所示。 表单本身是渲染,所以我在index.js中的工作正在进行中。 我是否需要在其他地方添加一些额外的代码,或者我错过了一些额外的代码。 我有nodemailer等,也安装了所有必需的软件包。 我对此非常新,我将不胜感激任何帮助。 谢谢!

404 error

app.js

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var nodemailer = require('nodemailer');
var index = require('./routes/index');
var users = require('./routes/users');
var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');


// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', index);
app.use('/users', users);


// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

app.post('/contactus', function (req, res) {
      var mailOpts, smtpTrans;
      //Setup Nodemailer transport, I chose gmail. Create an application-specific password to avoid problems.
      smtpTrans = nodemailer.createTransport('SMTP', {
        service: 'Gmail',
        auth: {
          user: "me@gmail.com",
          pass: "xxxxxx"
        }
      });
      //Mail options
      mailOpts = {
        from: req.body.name + req.body.email,
        to: 'yyyyyyyyyy@gmail.com',
        subject: req.body.email + '  --Msg from contactus-form',
        text: "Name: " + req.body.name + "Email: "  + req.body.email + 
              "Contact No:  " + req.body.contactNo + "QUERY: " + req.body.message
      };
      smtpTrans.sendMail(mailOpts, function (error, response) {
        //Alert on event of message sent succeeds or fail.
        if (error) {
          res.render('contactus',{msg : 'Error occured, message not sent.', err : true});
        }
        else {
          res.render('contactus',{msg : 'Message sent! Thank you.', err : false});
        }
        smtpTrans.close();
      });
});
// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});


module.exports = app;

contactus.hbs

<html>
  <head>
    <title></title>
    <link rel="stylesheet" href="/stylesheets/style.css">
  </head>
  <body>
    <div class="content">
      <form action="/contactus" method="post">
        <fieldset>
          <p>
            <input type="text" name="name" size="30" required="" placeholder="Name:">
          </p>
          <p>
            <input type="text" name="contactNo" size="30" placeholder="Contact No.:">
          </p>
          <p>
            <input type="text" name="email" size="30" required="" placeholder="Email:">
          </p>
          <p><br>
            <textarea type="text" name="message" cols="37" rows="7" size="30" placeholder="Your message please"></textarea>
          </p>
          <p>
            <input name="submit" type="submit" value="Send" id="submit">
          </p><br>
        </fieldset>
      </form>
    </div>
  </body>
</html>

1 个答案:

答案 0 :(得分:-1)

此处理程序:

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

应该移到500错误处理程序之前。

原因是,它充当了一切。这意味着如果它之前的某些东西没有首先路由某个地方,它将始终执行。因此它会在有机会到达contactus路径之前运行并发送404。