0

I have a basic nodejs server serving static files. It works very well in local tests.

However, I hosted it on a google cloud instance. Set up a nginx server. Accessing the page works fine.

The problem is that some included js files in the html page are not found when accessing the page from the google hosted server. I checked the code/file, it is the same as the one used in local tests.

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 page:

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

Error: http://prntscr.com/jmue8t

File architecture on the instance: http://prntscr.com/jmuebs

Why is it doing that and how to solve that?

Edit: NGINX configuration file (server_name is my instance external IP):

http://prntscr.com/jmy8wy

Itération 122442
  • 2,644
  • 2
  • 27
  • 73

1 Answers1

1

Your files will be available at http://you-ip-address/public/web3-min.js

In your error image, you are accessing them like http://you-ip-address/web3-min.js.

So, in your HTML page change src="/public/web3-min.js"

Edit:

If you want Nginx to handle the serving of static file you can configure your Nginx like below.

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;
  }
}

Ref: Express + Nginx. Can't serve static files

BeeBee8
  • 2,944
  • 1
  • 27
  • 39