制作中のホームページの問い合わせフォームで、nodemailerでGmailを送信させたあとに、「メッセージは送信されました」と表記されたページ('/aftercontact')へredirectしたいのですが、されません。
メールはちゃんと送信されるのですが、その後の処理がなされないようでページ移動することなく、無限ループが始まりredirectは処理されません。
該当部のコードは以下の通りです。
node.js
1const transporter = nodemailer.createTransport({ 2 host: "smtp.gmail.com", 3 port: 465, 4 secure: true, 5 auth: { 6 type: 'OAuth2', 7 user: process.env.EMAILADDRESS, 8 pass: process.env.EMAIL_PASSWORD, 9 clientId: process.env.CLIENTID, 10 clientSecret: process.env.CLIENT_SECRET, 11 refreshToken: process.env.REFRESH_TOKEN, 12 } 13}); 14 15 16app.post('/contact', (req, res, next) => { 17 const username = req.body.username; 18 const useremail = req.body.useremail; 19 const usermessege = req.body.usermessege; 20 const errors = []; 21 22 23 if (username === '') { 24 errors.push('お名前がありません!'); 25 } 26 if (useremail === '') { 27 errors.push('メールアドレスがありません!'); 28 } 29 if (usermessege === '') { 30 errors.push('メッセージがありません!'); 31 } 32 if (errors.length > 0) { 33 res.render('contact.ejs', { errors: errors }); 34 } else { 35 next(); 36 } 37}, 38 (req, res) => { 39 const username = req.body.username; 40 const useremail = req.body.useremail; 41 const usermessege = req.body.usermessege; 42 const mailOptions = { 43 from: process.env.EMAILADDRESS, 44 to: process.env.EMAILADDRESS2, 45 subject: '問い合わせ', 46 text: 'From:' + ' ' + username + ' ' + '/Email:' + ' ' + useremail + ' ' + '/Message:' + ' ' + usermessege 47 }; 48 49 transporter.sendMail(mailOptions, function (error, info) { 50 if (error) { 51 console.log(error); 52 } else { 53 console.log(Email_sent); 54 res.redirect('/aftercontact'); 55 } 56 }); 57 58 });
ページにredirectされないだけではなく、ターミナルにconsole.log(Email_sent)されないので、何かしら問題があるのでしょうがわかりません。
('/aftercontact')のページは’localhost:3000/aftercontact’で表示されるので、('/aftercontact')ページ自体には問題ないと思うのですが、クロームのデベロッパーツールを使うと、
Failed to load resource: the server responded with a status of 404 (Not Found) send url.js:1
と表示されるので、転送先urlが認識されていないようです。
全てのページファイルは同じディレクトリに保存されています。
何か原因があるのでしょうか?

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。