1

我已经使用 aws eb deploy 部署了 nodejs 应用程序,client.js 没有加载到 index.hbs 文件中并返回 404 错误(加载资源失败:服务器响应状态为 404(未找到))

我已经尝试了互联网上的所有步骤,为此浪费了两天时间。

任何人都可以在这里帮助我吗?我在这里做错了什么?

我已经在这个stackover flow page Elastic Beanstalk Static Folder 403 Error中尝试了解决方案,但没有运气

staticfiles.config (NodejsPlayGround.elasticbeanstalk\staticfiles.config) 文件

选项设置:

aws:elasticbeanstalk:container:nodejs:staticfiles:
/public: public

index.hbs (NodejsPlayGround\views\index.hbs) 文件

<!DOCTYPE html>
<html>
<head>
<title>NodeSeverTest</title>
<script src="js/client.js"></script>
</head>
<body>
<h1>Hello</h1>
</body>
</html>

server.js (NodejsPlayGround\server.js) 文件

'use strict';
const express = require('express');
const path = require('path');

const app = express();
const port = process.env.PORT || 3000;

const views_path = path.join(__dirname, 'views');
const static_path = path.join(__dirname, 'public')

app.set('view engine', 'hbs');
app.set('views', views_path);

app.get('', (req, res) => {
res.render('index', {
title: 'Weather App',
name: 'ArunKumar Arjunan'
}
);
});
app.listen(port, () => {
console.log('server started on port ' + port);
});

client.js (NodejsPlayGround\public\js\client.js) 文件:

console.log('client side java script file is loaded');

client.js 文件需要加载到 index.hbs 文件中

4

1 回答 1

0

我发现与 AWS 文档相反,文件夹路径还需要一个前导斜杠 - 即您映射/public/public(不仅仅是public),或者映射/static到(/static不仅仅是static)。

我通过检查我的 EB 实例上的 nginx 错误日志和 conf 文件发现了这一点,两者都表明它正在寻找我的静态文件/var/app/currentstatic而不是/var/app/current/static.

于 2020-04-15T13:22:04.600 回答