直接表达到生产中的错误端点,但可以完美地在开发中工作

问题描述 投票:2回答:1

直接在生产环境中表达到错误的端点,但是在开发中可以正常工作。我已经使用express为后端构建了应用程序,并为前端和护照进行了身份验证,现在这里我遇到了端点/auth/google的问题。当我单击按钮时,它应直接表示端点auth,但应直接响应未找到的应用程序组件。

仅是我的应用程序未命中端点auth/google而是渲染了反应页面

这里是代码

server.js

app.use('/auth', require('./router/auth')) // should direct here
app.use('/media', require('./router/media')) 
app.use('/admin', require('./router/admin')) 
app.use('/user', require('./router/user'))

const httpServer = http.createServer(app)

if (process.env.NODE_ENV === 'production') {
 app.use(favicon(path.join(__dirname, '../' + 'build', 'favicon.ico')))

 app.use(express.static(path.join(__dirname, '../' + 'build')));

 app.get("*", (req, res) => { // but always goes here
 res.sendFile(path.join(path.join(__dirname, '../' + 'build', 'index.html')));
  });
}

const PORT = 8080

httpServer.listen(PORT, () => {
 console.log('Server up at:' + PORT)
})

/ router / auth.js

router.get('/google', passport.authenticate('google', { // and should hit this 
  scope: ['profile', 'email']
}))
router.get(
  '/google/callback',
  passport.authenticate('google'),
  (req, res) => {
    req.app.set('user', res.req.user)
    return res.redirect('/auth/sign')
  }
)

module.exports = router


 <Switch>
          <Route path="/" exact component={Main} />
          <Route path="/home" exact component={Home} />
          <Route path="/ad/:id" exact component={Ad} />
          <PrivateRoute path="/postad" exact component={createAd} />
          <PrivateRoute path="/ad/edit/:id" exact component={UpdateAd} />
          <Route path="/user/:id" exact component={User} />
          <PrivateRoute path="/setting" exact component={Setting} />
          <PublicRoute path="/sign" exact component={ProviderSign} />
          <Route path="*" exact={true} component={PageNotFound} /> // but render this
 </Switch>

TLDR

设置为"proxy": "http://localhost:8080"时,我也被重定向到反应页面,而在客户端src文件夹中找到此http-proxy-middleware和设置代理后,>

const proxy = require("http-proxy-middleware");

module.exports = app => {
  app.use(proxy("/auth/google", { target: "http://localhost:8080/" }));
  app.use(proxy("/auth/facebook", { target: "http://localhost:8080/" }));
};

在我在端口8080上启动节点服务器并在端口3000上启动客户端时,此工作正常,

这是我的登录页面按钮,将击中端点/auth/google

<Button className={classes.authBtn}> 
 <a className={classes.removeStyle} href="/auth/google">Google</a>     
</Button>

直接在生产环境中表达到错误的端点,但是在开发中可以正常工作。我已经使用express为后端构建了我的应用程序,并为前端使用了反应,为...

javascript node.js reactjs express passport.js
1个回答
0
投票

我认为这可能是您的问题。您只是将护照身份验证器用作中间件。因此,实际上发生的事情是护照只在拨打next()

© www.soinside.com 2019 - 2024. All rights reserved.