node.js - 如何在 AWS Bitnami 服务器上使用 nodejs express 发送静态文件(CSS、js)?
问题描述
我正在通过 AWS 使用 bitnami 开发 MEAN 应用程序,但这是我第一次这样做。我不知道如何发送公共文件夹中的静态文件,例如 css 和 js。
我觉得我已经尝试了配置 apache 代理和在路径中包含或不包含“public”的所有组合,但我必须从根本上遗漏一些东西。我已经查看了有关此主题的其他问答,但我认为我在下面尝试的内容使用了这些方法。不知道它是否与代理有关,因为我将http://public-ip/test3转发到节点服务器。
bitnami-apps-prefix.conf 文件:
ProxyPass /test3 http://127.0.0.1:3100/
ProxyPassReverse /test3 http://127.0.0.1:3100/
nodejs app.js 文件 - 在各个地方都没有足够的公众尝试过
var express = require("express");
const path = require('path');
var app = express();
app.use(express.static(path.join(__dirname, '/public')));
app.get("/", function(req,res){
res.sendFile(__dirname+"/public/p4.1.html");
});
app.listen(3100,'localhost',function(){
console.log("server has started");
});
p4.1.html 文件 - 我已经在路径中有和没有公共的情况下尝试过它。
<html><head>
<link rel="stylesheet" href="public/styles.css">
<script src="public/functions.js"></script>
</head>
p4.1.html、styles.css 和 functions.js 存储在 public 文件夹中。公共文件夹与 app.js 文件的路径相同。
html 页面确实显示,但 css 和 js 出现 404 错误:http://public-ip/public/functions.js net::ERR_ABORTED 404 (Not Found)
这是我的代码还是代理中的东西?任何帮助表示赞赏。
解决方案
所以解决办法是在html文件的url中放test3而不是public。
<html><head>
<link rel="stylesheet" href="test3/styles.css">
<script src="test3/functions.js"></script>
</head>
推荐阅读
- bash - 如何将循环变量作为输入传递给剪切?
- javascript - 使用 asyncStorage 可点击 React Native 标头
- webgl - Webgl:销毁或重用缓冲区/纹理?
- c++ - C++ Lambda 作为 CRTP 类中的静态成员无法在 Visual Studio 15 中编译
- nested - 重复字段中的大查询更新结构为空
- python-3.x - Tkinter 中的工具栏(python3)
- javascript - AngularJS:断开表单与其父级之间的连接
- ios - iOS / Swift - 附加过滤器以从 Firebase 检索数据
- .net - 是否可以简化迁移?
- javascript - 搜索和替换 html 元素值的最佳方法是什么