首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >and + Express + Nodemailer:不能同时呈现组件和Nodemailer

and + Express + Nodemailer:不能同时呈现组件和Nodemailer
EN

Stack Overflow用户
提问于 2017-07-28 08:10:37
回答 1查看 493关注 0票数 2

我想呈现我的反应应用程序与客户端路由,我想发送一封邮件与Nodemailer。因为Nodemailer不能在客户端使用,所以我必须在Express服务器上实现它。这就是服务器的样子:

express.js

代码语言:javascript
复制
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”,则会发送邮件,但我的应用程序不会呈现。

此外,我还尝试了以下几种方法:

代码语言:javascript
复制
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'))
  }
});

有解决办法吗?

EN

回答 1

Stack Overflow用户

发布于 2017-07-28 11:17:21

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

代码语言:javascript
复制
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
})
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45368196

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档