首页 > 解决方案 > 如何在 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)

这是我的代码还是代理中的东西?任何帮助表示赞赏。

标签: node.jsamazon-web-servicesexpressmean-stackbitnami

解决方案


所以解决办法是在html文件的url中放test3而不是public。

<html><head>
<link rel="stylesheet" href="test3/styles.css">
<script src="test3/functions.js"></script>
</head>

推荐阅读