gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 13:37:52 26 4
gpt4 key购买 nike

在生产环境中直接指向错误的端点,但在开发中工作正常。我使用express作为后端构建了我的应用程序,使用react作为前端,并使用passport进行身份验证,现在我面临端点/auth/google的问题。当我单击按钮时,它应该定向到express端点auth,但express定向到 react 应用程序未找到组件。

只是我的应用程序没有命中端点auth/google而是渲染 react 页面

这里是代码

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

passport.js

export default function (passport) {
passport.serializeUser(function (user, done) {
done(null, user)
})

passport.deserializeUser(function (user, done) {
done(null, user)
})

// GOOGLE OAuth
passport.use(
new GoogleStrategy(
{
clientID: GOOGLE_CLIENT_ID,
clientSecret: GOOGLE_CLIENT_SECRET,
callbackURL: '/auth/google/callback'
},
function (_, __, profile, done) {
profile = {
...profile,
email: profile.emails && profile.emails[0].value,
profileUrl: profile.photos && profile.photos[0].value
}
authUser(profile, done) // function for save user
}
)
)
}

react app.js


<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”时,我的页面也重定向到 react 页面,并且在我找到这个http-proxy-middleware<之后 并在客户端 src 文件夹上设置代理

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 上启动 Node 服务器并在端口 3000 上启动客户端时,此功能正常工作,

这是我的登录页面按钮,用于访问端点 /auth/google

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

最佳答案

对我来说,一个解决方案是创建一个 routes.js 文件,例如:

const express = require("express");
const router = express.Router();

const authRouter = require('./router/auth');
const mediaRouter = require('./router/media');
const adminRouter = require('./router/admin');
const userRouter = require('./router/user');

router.get("/", function(req, res, next) {
res.status(200).json({
isSuccess: true,
message: "Server is up and running!"
});
});

app.use('/auth', authRouter);
app.use('/media', mediaRouter);
app.use('/admin', adminRouter);
app.use('/user', userRouter);

router.get("*", (req, res) => {
res.status(200).json({
isSuccess: false,
message: "Invalid address!"
});
});

module.exports = router;

将您的 server.js 文件修改为:

const httpServer = http.createServer(app);
const indexRouter = require("./routes"); // your routes.js

app.use("/api", indexRouter);

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)
})

最后将您的 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('api/auth/sign') // Changed this endpoint
}
)

module.exports = router

这种方法将您的 API 和前端路由分开。希望这对您有用。

关于javascript - 在生产中直接表达到错误的端点,但在开发中完美工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60094357/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com