【问题标题】:Behaviour of node in local and prod节点在本地和产品中的行为
【发布时间】:2018-11-05 14:41:49
【问题描述】:

我有一个提供静态文件的基本 nodejs 服务器。它在本地测试中运行良好。

但是,我将它托管在谷歌云实例上。设置一个 nginx 服务器。访问该页面工作正常。

问题是从谷歌托管服务器访问页面时,html页面中一些包含的js文件没有找到。我检查了代码/文件,它与本地测试中使用的相同。

let express = require('express');
var app = express();
app.use(express.static('public'));
app.get('/',function(req,res){

    res.sendFile(__dirname + '/main.html');

});

app.listen(8080);

console.log("listenning on 8080");

HTML 页面:

<head>
    <script type="text/javascript" src="/web3-min.js"></script>
    <script type="text/javascript" src="/etherjs.js"></script>

错误:http://prntscr.com/jmue8t

实例上的文件架构:http://prntscr.com/jmuebs

为什么要这样做以及如何解决?

编辑:NGINX 配置文件(server_name 是我的实例外部 IP):

http://prntscr.com/jmy8wy

【问题讨论】:

  • 包括你的 nginx 配置
  • @BrahmaDev 更新

标签: javascript node.js nginx google-compute-engine


【解决方案1】:

您的文件将在http://you-ip-address/public/web3-min.js 上提供

在您的错误图像中,您可以像http://you-ip-address/web3-min.js 一样访问它们。

所以,在您的 HTML 页面中更改 src="/public/web3-min.js"

编辑:

如果你想让 Nginx 处理静态文件的服务,你可以像下面这样配置你的 Nginx。

server {
  listen 8081;
  server_name example.com;
  location / {
    root /path/to/website/public;
    index index.html;
    try_files $uri $uri/ @express; # instead of 404, proxy back to express using a named location block;
    # source: https://stackoverflow.com/a/15467555/8436941
  }
  location @express {
    proxy_pass http://localhost:8080;
  }
}

参考:Express + Nginx. Can't serve static files

【讨论】:

  • 更新后的源代码仍然存在完全相同的问题。
  • 错误是否更改为 http://you-ip-address/public/web3-min.js ?换源后?
  • 是的。对不起,忘了提这个。源 you-ip-address/public/web3-min.js 也不起作用。
  • stackoverflow.com/questions/46692341/… 看看这对你有没有帮助。
  • 第二个配置为我做了。感谢您的时间。也许您可以更新您的答案以指定我在您提到的链接中得到了我的解决方案?
猜你喜欢
  • 1970-01-01
  • 2020-11-08
  • 2018-05-25
  • 1970-01-01
  • 1970-01-01
  • 2018-07-09
  • 2021-01-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多