繁体   English   中英

Express:使用URL参数时服务器无法处理刷新

[英]Express: server unable to handle refresh when using URL param

当使用URL参数时,例如books/1 ,由于某些原因,快速应用无法找到并提供Index.js

我正在使用绝对路径,但是即使使用参数在URL刷新时,我的express应用程序仍然无法找到Index.js

找不到Index.js

正如您在屏幕快照中看到的那样,它无法找到Index.js而不是提供它,而是将HTML文件替换为Index.js

我正在做全面解决方案 ,该方法非常有效,直到在url参数处刷新为止:

app.use(express.static(path.resolve(__dirname,  '..', 'build')));
app.get('/*', (req, res) => {
 res.sendFile(path.join(__dirname, '..', 'build', 'index.html'));
});

我什至尝试为/Index.js设置特定的路由,但是没有运气。

app.use(express.static(path.resolve(__dirname,  '..', 'build')));
app.get('/Index.js', (req, res) => {
 res.sendFile(path.join(__dirname, '..', 'build', 'Index.js'))
});
app.get('/*', (req, res) => {
 res.sendFile(path.join(__dirname, '..', 'build', 'index.html'));
});

我也尝试过使用serve-static而不是express.static但是并没有帮助。

如果有人有兴趣看一下,我已经在这个仓库中重复了这个问题。

这个项目的前端是理性的

这可能很简单,但是您是否尝试过: res.sendFile('../public/index.js', {root: __dirname});

在安装路径“ book / Index.js”上提供Index.js可解决此问题。

像这样:

app.use(express.static(path.resolve(__dirname,  '..', 'build')));
app.get('/book/Index.js', (req, res) => {
 res.sendFile(path.join(__dirname, '..', 'build', 'Index.js'));
});
app.get('/*', (req, res) => {
 res.sendFile(path.join(__dirname, '..', 'build', 'index.html'));
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM