javascript - React + Express + Nodemailer: Can't render components and Nodemailer together -
i want render react app client-side routing , want send mail nodemailer. since nodemailer can't used on client-side have implement on express server. server looks like:
express.js
router.use(express.static(path.resolve(__dirname, '..', 'build'))) router.get('*', (req, res) => { res.sendfile(path.resolve(__dirname, '..', 'build/index.html')) }) router.get('/sendmail', (req, res) => { transporter.sendmail(options, (error, info) => { if (error){ console.log(error) } console.log('message send!') }) })
1) renders react components when route '/sendmail' blank page shown, index.html gets rendered without js , no mail sent.
2) if delete first line router.use(express.static...
, route '/sendmail' mail gets sent app won't render.
also i've tried following:
router.use(express.static(path.resolve(__dirname, '..', 'build'))) router.get('*', (req, res) => { console.log('path', req.path) if (req.path === '/sendmail') { transporter.sendmail(options, (error, info) => { if (error){ console.log(error) } console.log('message send!') }) } else { res.sendfile(path.resolve(__dirname, '..', 'build/index.html')) } });
any solution?
just change order of routes in express, matched top bottom:
router.use(express.static(path.resolve(__dirname, '..', 'build'))) router.get('/sendmail', (req, res) => { // processed when send /sendmail express server .... } router.get('*', (req, res) => { res.sendfile(path.resolve(__dirname, '..', 'build/index.html')) // match if request not /sendmail })
Comments
Post a Comment