Node.js quick file server (static files over HTTP)

2019-01-01 14:01发布

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

27条回答
一个人的天荒地老
2楼-- · 2019-01-01 14:37

Here's another simple web server.

https://www.npmjs.com/package/hostr

Install

npm install -g hostr

Change working director

cd myprojectfolder/

And start

hostr
查看更多
几人难应
3楼-- · 2019-01-01 14:37

I use Houston at work and for personal projects, it works well for me.

https://github.com/alejandro/Houston

查看更多
查无此人
4楼-- · 2019-01-01 14:38

You can try serve-me

Using it is so easy:

ServeMe = require('serve-me')();
ServeMe.start(3000);

Thats all.

PD: The folder served by default is "public".

查看更多
若你有天会懂
5楼-- · 2019-01-01 14:39

For the benefit of searchers, I liked Jakub g's answer, but wanted a little error handling. Obviously it's best to handle errors properly, but this should help prevent a site stopping if an error occurs. Code below:

var http = require('http');
var express = require('express');

process.on('uncaughtException', function(err) {
  console.log(err);
});

var server = express();

server.use(express.static(__dirname));

var port = 10001;
server.listen(port, function() { 
    console.log('listening on port ' + port);     
    //var err = new Error('This error won't break the application...')
    //throw err
});
查看更多
听够珍惜
6楼-- · 2019-01-01 14:40

There is another static web server that is quite nice: browser-sync.

It can be downloaded using node package manager:

npm install -g browser-sync

After installation, navigate to the project folder in the cmd prompt and just run the following:

browser-sync start --server --port 3001 --files="./*"

It will start catering all the files in the current folder in the browser.

More can be found out from BrowserSync

Thanks.

查看更多
还给你的自由
7楼-- · 2019-01-01 14:42

First install node-static server via npm install node-static -g -g is to install it global on your system, then navigate to the directory where your files are located, start the server with static it listens on port 8080, naviaget to the browser and type localhost:8080/yourhtmlfilename.

查看更多
登录 后发表回答