Is there Node.js ready-to-use tool (installed with npm
), that would help me expose folder content as file server over HTTP.
Example, if I have
D:\Folder\file.zip
D:\Folder\file2.html
D:\Folder\folder\file-in-folder.jpg
Then starting in D:\Folder\
node node-file-server.js
I could access file via
http://hostname/file.zip
http://hostname/file2.html
http://hostname/folder/file-in-folder.jpg
Why is my node static file server dropping requests? reference some mystical
standard node.js static file server
If there's no such tool, what framework should I use?
Related: Basic static file server in NodeJS
A simple Static-Server using connect
See also Using node.js as a simple web server
You also asked why requests are dropping - not sure what's the specific reason on your case, but in overall you better server static content using dedicated middleware (nginx, S3, CDN) because Node is really not optimized for this networking pattern. See further explanation here (bullet 13): http://goldbergyoni.com/checklist-best-practice-of-node-js-in-production/
A good "ready-to-use tool" option could be http-server:
To use it:
Or, like this:
Check it out: https://github.com/nodeapps/http-server
If you do not want to use ready tool, you can use the code below, as demonstrated by me at https://developer.mozilla.org/en-US/docs/Node_server_without_framework:
UPDATE If you need to access your server from external demand/file, you need to overcome the CORS, in your node.js file by writing the below, as I mentioned in a previous answer here
UPDATE
As Adrian mentioned, in the comments, he wrote an ES6 code with full explanation here, I just re-posting his code below, in case the code gone from the original site for any reason:
Install express using npm: https://expressjs.com/en/starter/installing.html
Create a file named server.js at the same level of your index.html with this content:
If you wish to put it in a different location, set the path on the third line:
CD to the folder containing your file and run node from the console with this command:
Browse to localhost:8080
If you use the Express framework, this functionality comes ready to go.
To setup a simple file serving app just do this: