简体   繁体   中英

Nodemailer doesn't work from routes folder

Im having an issue where my subscribe form works perfect if I call it from app.js where is express server defined, but if I move subscribe.js to routes folder, I cannot send mail. I connected both files but still doesnt work. When I press send button it gives me error "Not Found" (localhost:3000/send)

Here is my app.js code

var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var ejs = require('ejs');

var indexRouter = require('./routes/index');
var subscribeRouter = require('./routes/subscribe');

var app = express();

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

app.use(express.static(path.join(__dirname, 'public')));

app.use('/', indexRouter);
app.use('/subscribe', subscribeRouter);

module.exports = app;

and my subscribe.js code

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

var router = express.Router();

/* GET Subscribe Page. */
  router.get('/', (req, res) => {
  res.render('subscribe');
});

// Body Parser Middleware
router.use(bodyParser.urlencoded({ extended: false }));
router.use(bodyParser.json());

router.post('/send', (req, res) => {
  const output = `
    <p>....</p>
    <h3>...</h3>
    <ul>  
        <li>E-naslov: ${req.body.email}</li>
    </ul>
  `;

// create reusable transporter object using the default SMTP transport
  let transporter = nodemailer.createTransport({
    host: '...'
    port: 465,
    secure: true, // true for 465, false for other ports
    auth: {
        user: 'example@example.com', // generated ethereal user
        pass: 'xxxxxxxx'  // generated ethereal password
    },
    tls:{
      rejectUnauthorized:false
    }
  });

// setup email data with unicode symbols
  let mailOptions = {
      from: '"blabla"<info@blabla.com>', // sender address
      to: 'example@gmail.com', // list of receivers
      subject: 'Novo naročilo na e-novice', // Subject line
      text: 'Hello world?', // plain text body
      html: output // html body
  };

// send mail with defined transport object
  transporter.sendMail(mailOptions, (error, info) => {
      if (error) {
          return console.log(error);
      }
      console.log('Message sent: %s', info.messageId);   
      console.log('Preview URL: %s', nodemailer.getTestMessageUrl(info));

      res.render('subscribe', {msg:'Email has been sent'});
  });
});

module.exports = router;

in my subscribe.ejs my form has action=/send, and method=post

I think your route is actually /subscribe/send

When you use the subscribe router in index you are mapping it to /subscribe and in that router /send is relative.

use

app.use(subscribeRouter);

instead of

app.use('/send', subscribeRouter);

UPDATE

I've changed the code above to use the route /send and make sure subscribe.ejs exists in your views directory

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM