所以我认为这首先是一个Heroku问题,但是使用NodeJS在本地运行时也会发生同样的事情。

我的Angular应用程序的主页显示正常,并且使用链接导航时,路由可以正常工作。

但是,如果我尝试刷新路由上的页面(例如/ login),则服务器仅以以下文本作为响应:
/app/dist/meal-planner/index.html在Heroku上,以及
本地/Users/name-here/Development/workspace/meal-planner/dist/meal-planner/index.html

这是我的server.js:

//Install express server
const express = require('express');
const http = require('http');
const path = require('path');

const app = express();

// Serve only the static files form the dist directory
app.use(express.static(path.join(__dirname, '/dist/meal-planner')));

// For all GET requests, send back index.html (PathLocationStrategy)
app.get('*', (req,res) => {
    res.send(path.join(__dirname, '/dist/meal-planner/index.html'));
});

// Start the app by listening on the default Heroku port
const port = process.env.PORT || 8080;
app.set('port', port);
const server = http.createServer(app);
server.listen(port, () => console.log('Running on port ' + port));


还有我的folder structure以防万一...

最佳答案

res.send(path.join(__dirname, '/dist/meal-planner/index.html'));


path.join返回一个字符串,您正在将该字符串作为响应发送。这解释了服务器为何以文本响应。

您可能需要sendFile而不是send

res.sendFile(path.join(__dirname, '/dist/meal-planner/index.html'));

07-26 05:52
查看更多