React + Express + Nodemailer:无法将组件和Nodemailer渲染在一起

我想使用客户端路由来呈现我的React应用程序,并且想要使用Nodemailer发送邮件。 由于Nodemailer不能在客户端使用,我必须在Express服务器上实现此function。 这就是服务器的样子:

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 was send!') }) }) 

1)所以这呈现了React组件,但是当我路由到'/ sendmail'时显示一个空白页面,index.html被渲染为没有js,并且没有邮件被发送。

2)如果我删除第一行router.use(express.static...并路由到'/ sendmail'邮件被发送,但我的应用程序不会呈现。

另外我已经尝试了以下内容:

 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 was send!') }) } else { res.sendFile(path.resolve(__dirname, '..', 'build/index.html')) } }); 

任何解决scheme

只要改变快递路线的顺序,就会从上到下匹配:

 router.use(express.static(path.resolve(__dirname, '..', 'build'))) router.get('/sendmail', (req, res) => { // this will be processed when you send a GET for /sendmail to your express server .... } router.get('*', (req, res) => { res.sendFile(path.resolve(__dirname, '..', 'build/index.html')) // this will always match if the request is not /sendmail })