我想呈现我的反应应用程序与客户端路由,我想发送一封邮件与Nodemailer。因为Nodemailer不能在客户端使用,所以我必须在Express服务器上实现它。这就是服务器的样子:
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'))
}
});有解决办法吗?
发布于 2017-07-28 11:17:21
只要改变特快路线的顺序,它们就会从上到下匹配:
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
})https://stackoverflow.com/questions/45368196
复制相似问题