简体   繁体   English

AWS Elastic Beanstalk 不会从 nodejs 应用程序的公共文件夹加载静态文件

[英]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.

相关问题 在 Elastic Beanstalk AWS 中为 nodeJs 应用程序提供静态文件 - Serve static files for a nodeJs application in Elastic Beanstalk AWS AWS Elastic Beanstalk - NodeJS:在没有 Beanstalk 负载均衡器的情况下从 Letsencrypt 获取证书 SSL - AWS Elastic Beanstalk - NodeJS : Get certificate SSL from Letsencrypt without Beanstalk Load Balancer 从弹性beantalk提供静态文件不起作用 - Serving static files from elastic beanstalk not working nodejs express 应用程序从 public/js 文件夹中提供静态文件不起作用 - nodejs express app serve static files from public/js folder not work AWS Elastic Beanstalk NodeJS 和日志 - AWS Elastic Beanstalk NodeJS and logs 将nodejs应用程序部署到AWS弹性beantalk时出现严重依赖的错误 - ERROR with sharp dependency when deploying nodejs app to AWS elastic beanstalk 如何从AWS Elastic BeanStalk获取更新的``上传&#39;&#39;文件夹? - How to get the updated 'uploads' folder from AWS Elastic BeanStalk? 从 VSCode 将 NodeJS 应用程序部署到 Elastic Beanstalk Docker 平台? - Deploy NodeJS App to Elastic Beanstalk Docker Platform from VSCode? AWS Elastic BeanStalk nodejs部署错误 - AWS Elastic BeanStalk nodejs Deployment error 使用 Elastic Beanstalk 在 AWS 上部署 NodeJS API - NodeJS API deployement on AWS with Elastic Beanstalk
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM