【发布时间】:2018-09-04 23:02:12
【问题描述】:
我想为路由“/about”提供另一个静态页面, 现在我有:
app.use(express.static(path.resolve(__dirname, '..', 'build')));
app.get('/', (req, res) => {
res.sendFile(path.resolve(__dirname, '..', 'build', 'index.html'));
});
app.get('/about', function (req, res) {
console.log("here")
res.sendFile(path.resolve(__dirname, '..', 'build', 'about.html'));
});
我也有发帖请求
app.post('/getfrontpage', (req, res) => {
console.log("FIRST PAGE", req.body.params.page)
});
post 请求有效,我用 axios 拨打电话,但是当我更改 url 时“/about”路由不起作用
【问题讨论】:
-
嗨,很难说。我看到你有一个反应前端。如果不使用哈希路由,您的反应前端路由必须与 express api 完全相同。另一种选择可能是将您的 api 函数数组传递给 '/' 上的 app.use 并使用 next() 调用堆栈中的下一个函数。 - 真相就在那里
-
您也可以尝试让另一个 app.use 在 '/about' 路由上专门为静态文件提供服务,然后使用 next() 调用回调以发送文件。希望这可以帮助。 - 真相就在那里
标签: reactjs express static routes router