[英]AWS Elastic Beanstalk wont load static files from public folder for nodejs app
I have deployed the nodejs application with aws eb deploy, client.js is not getting loaded in index.hbs files and returning 404 error (Failed to load resource: the server responded with a status of 404 (Not Found))我已经使用 aws eb deploy 部署了 nodejs 应用程序,client.js 未加载到 index.hbs 文件中并返回 404 错误(无法加载资源:服务器响应状态为 404(未找到))
I have tried all the step in the internet and wasted two days for this.我已经尝试了互联网上的所有步骤,为此浪费了两天时间。
Can any one please help me here ?任何人都可以在这里帮助我吗? I am doing anything wrong here?
我在这里做错了什么?
I have tried the solution in this stackover flow page Elastic Beanstalk Static Folder 403 Error but no luck我已经在这个堆栈流页面Elastic Beanstalk 静态文件夹 403 错误中尝试了解决方案,但没有运气
staticfiles.config (NodejsPlayGround.elasticbeanstalk\\staticfiles.config) file staticfiles.config (NodejsPlayGround.elasticbeanstalk\\staticfiles.config) 文件
option_settings:选项设置:
aws:elasticbeanstalk:container:nodejs:staticfiles:
/public: public
index.hbs (NodejsPlayGround\\views\\index.hbs) file 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) file 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) file: client.js (NodejsPlayGround\\public\\js\\client.js) 文件:
console.log('client side java script file is loaded');
client.js file needs to be loaded in index.hbs file client.js 文件需要加载到 index.hbs 文件中
I find that contrary to the AWS documentation on this, the folder path also needs a leading slash — ie you map /public
to /public
(not just public
), or /static
to /static
(not just static
).我发现与 AWS 文档相反,文件夹路径还需要一个前导斜杠 - 即您将
/public
映射到/public
(不仅仅是public
),或/static
到/static
(不仅仅是static
)。
I figured this out by inspecting the nginx error log and conf files on my EB instances, and both showed that it was looking for my static files in /var/app/currentstatic
instead of /var/app/current/static
.我通过检查 EB 实例上的 nginx 错误日志和 conf 文件发现了这一点,两者都表明它正在
/var/app/currentstatic
而不是/var/app/current/static
查找我的静态文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.