Friday, 7 April 2017

NPM package 'http-server' seems to 404 arbitrary asset paths

I have a simple folder that I want to serve to localhost as a bunch of static assets. The file structure looks something like this:

  • build
    • assets
    • data
    • images
    • index.html

I installed http-server via NPM using npm install http-server -g and I'm serving this directory (build) full of static files to localhost using:

cd ~/MyProject/build
http-server .

The result is that I can visit localhost:8080 in my browser and I get index.html loading as expected. However, it loads incompletely, and in the console I see that a JS file (in the /assets path) referenced in index.html is loading and running correctly, but http-server is returning 404 for relative path assets in the /data path. In other words, the following browser console error:

GET http://localhost:8080/data/myFile.json 404 (Not Found)

Attempting to reach this asset by direct path also fails, e.g. by typing in my browser address bar:

http://localhost:8080/data/myFile.json

However, if I move this asset into the /images path (just as an experiment) then it works. In other words, the following in the browser address bar does return the desired file:

http://localhost:8080/images/myFile.json

Also tested by installing ExpressJS in this project, and creating a simple NodeJS script configured to serve the 'build' directory (same physical directory with no file structure or naming alterations). It worked perfectly with no path or filename alterations, so I know the file structure and names are good. Something is fishy just with http-server. In the meantime, I'm just using ExpressJS as a solution, which is fine, but now I'm curious what the problem is in http-server because it's a very handy solution for the future if I can get it working!

Why can http-server find index.html and /assets path, but not /data path? Is the /data path illegal or have I done anything wrong? Is there a configuration I can alter in http-server to make this feasible? I can't rename the /data path because unalterable minified vendor JS refers to this path.



via Steverino

No comments:

Post a Comment