简体   繁体   English

在heroku应用程序中以express形式提供静态文件

[英]Serving static files in express in heroku app

I have two svelte (SPA) applications - one for admin and one for the public, and I'm trying to serve both of them with one express server.我有两个svelte (SPA) 应用程序 - 一个用于管理员,一个用于公众,我正在尝试使用一个快速服务器为这两个应用程序提供服务。 when I run this code on my localhost it's works fine, but in heroku I'm getting 404 on the static files of the admin page (the client app is fine).当我在本地主机上运行此代码时,它工作正常,但在 heroku 中,我在管理页面的静态文件上得到 404(客户端应用程序很好)。

my project structure: server.js client public build bundle client public build bundle我的项目结构: server.js client public build bundle client public build bundle

this is my html page that produces the invalid calls这是我产生无效调用的 html 页面

<head>
    <link rel='icon' type='image/png' href='/favicon.png'>
    <link rel='stylesheet' href='/admin/global.css'>
    <link rel='stylesheet' href='/admin/build/bundle.css'>
    <script defer src='/admin/build/bundle.js'></script>
</head>

this is my express routeing:这是我的快速路由:

app.get('/admin', utils.ensureToken, (req, res, next) => {
    app.use(express.static(path.join(__dirname, 'admin/public')));
    res.sendFile(path.resolve('admin', 'public', 'index.html'));
});

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

tried mixing the paths on express, but didn't seems to work.尝试在快递上混合路径,但似乎没有用。 thanks in advance.提前致谢。

At the end it was the package.json that was the problem.最后是 package.json 是问题所在。 forgot that the script "heroku-postbuild" needs to also run the "admin" npm start.忘了脚本“heroku-postbuild”还需要运行“admin”npm start。

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

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