问题描述
我每次在Google App Engine上都有一个angular和expressjs应用程序时,每次刷新时都会遇到404错误页面。我看到人们通过在app.yaml文件中使用处理程序来解决此问题,但是任何尝试都会破坏我的应用程序-在Express为Angular文件提供服务之前,有人必须这样做吗?
GAE上的项目文件夹结构 ./build Expressjs后端逻辑 ./public Angular 9前端(产品构建)
以下是我的Express Server
...
const app = express()
app.set('views','cloud/views') // Specify the folder to find templates
app.set('view engine','ejs') // Set the template engine
app.use(bodyParser.json({ limit: '50mb' }));
app.use(bodyParser.urlencoded({ limit: '50mb',extended: true }));
app.use('/backend',api)
// Serve static assets from the /public folder
app.use('/',express.static('./public'))
...
解决方法
feathersjs的express所提供的我的棱角分明的应用程序出现了此问题。 添加类似的内容可能会解决该问题:
app.use('/',express.static('./public',{ redirect: false }));
app.get('*',function (req,res,next) {
res.sendFile(path.join('./public','index.html'));
});