0

I'm using Express 4.10, nginx, ubuntu 16.04 LTS and I want to create a javascript folder in my public folder.

public/js/myfile.js

I see lot of question on stackoverflow about this but I always get a 404 on my javascript file.

Then here is my html code:

<script src="js/login.js" type="text/javascript"/>

And in my node.js file I have this :

app.use(express.static(path.join(__dirname, '/public')));

Here is my nginx node configuration

upstream node {
    server 127.0.0.1:3000;
    keepalive 8;
}

location ~ ^/(node|socket\.io) {
    proxy_pass http://example.com:3000;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
}

Thanks.

John
  • 4,711
  • 9
  • 51
  • 101
  • 1
    Did you try adding a `/` at the beginning of your `src` value? – mscdex Jul 25 '16 at 17:09
  • Yes but I have the same error . My server running on Nginx, maybe the problem is nginx side ? – John Jul 25 '16 at 17:30
  • @John did you remember to proxy nginx to node? http://stackoverflow.com/questions/5009324/node-js-nginx-what-now – m-a-r-c-e-l-i-n-o Jul 25 '16 at 17:32
  • @m-a-r-c-e-l-i-n-o I did, I edited my question with my nginx configuration – John Jul 25 '16 at 17:37
  • @John Is it just the js folder that is not being served or all files in the public directory? Also, is the public folder in the same folder as the script that contains the `app.use` declaration? – m-a-r-c-e-l-i-n-o Jul 25 '16 at 17:55
  • My problem is for all files in the public directory. – John Jul 25 '16 at 18:25
  • @John Is the "public" folder in the same folder as the node.js file that contains the `app.use()` declaration? – m-a-r-c-e-l-i-n-o Jul 25 '16 at 19:46
  • Yes they are in the same folder – John Jul 25 '16 at 19:58
  • 1
    Since you're proxying `/node` to your node process, did you try specifying that as the mount point for your static middleware?: `app.use('/node', express.static(path.join(__dirname, '/public')));` – mscdex Jul 25 '16 at 20:42
  • Oh thanks a lot this work very well :). Could you add you an anwser and I will accept it ? – John Jul 26 '16 at 09:30

1 Answers1

0

The script source should be something like /node/public/js/myfile.js, or else nginx would not pass the request to express.

Tony Yip
  • 705
  • 5
  • 14
  • The query is good when I'm loading my script `http://example.com/node/public/js/login.js` but I have a 404 on that – John Jul 25 '16 at 18:28