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

Take a look on that link.

You need only to install express module of node js.

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

app.use('/Folder', express.static(__dirname + '/Folder'));

You can access your file like http://hostname/Folder/file.zip

查看更多
忆尘夕之涩
3楼-- · 2019-01-01 14:26

One-line™ Proofs instead of promises

enter image description here

The first is http-server, hs - link

npm i -g http-server   // install
hs C:\repos            // run with one line?? FTW!!

The second is serve by ZEIT.co - link

npm i -g serve         // install
serve C:\repos         // run with one line?? FTW!!

Following are available options, if this is what helps you decide.

C:\Users\Qwerty>http-server --help
usage: http-server [path] [options]

options:
  -p           Port to use [8080]
  -a           Address to use [0.0.0.0]
  -d           Show directory listings [true]
  -i           Display autoIndex [true]
  -g --gzip    Serve gzip files when possible [false]
  -e --ext     Default file extension if none supplied [none]
  -s --silent  Suppress log messages from output
  --cors[=headers]   Enable CORS via the "Access-Control-Allow-Origin" header
                     Optionally provide CORS headers list separated by commas
  -o [path]    Open browser window after starting the server
  -c           Cache time (max-age) in seconds [3600], e.g. -c10 for 10 seconds.
               To disable caching, use -c-1.
  -U --utc     Use UTC time format in log messages.

  -P --proxy   Fallback proxy if the request cannot be resolved. e.g.: http://someurl.com

  -S --ssl     Enable https.
  -C --cert    Path to ssl cert file (default: cert.pem).
  -K --key     Path to ssl key file (default: key.pem).

  -r --robots  Respond to /robots.txt [User-agent: *\nDisallow: /]
  -h --help    Print this list and exit.
C:\Users\Qwerty>serve --help

  Usage: serve.js [options] [command]

  Commands:

    help  Display help

  Options:

    -a, --auth      Serve behind basic auth
    -c, --cache     Time in milliseconds for caching files in the browser
    -n, --clipless  Don't copy address to clipboard (disabled by default)
    -C, --cors      Setup * CORS headers to allow requests from any origin (disabled by default)
    -h, --help      Output usage information
    -i, --ignore    Files and directories to ignore
    -o, --open      Open local address in browser (disabled by default)
    -p, --port   Port to listen on (defaults to 5000)
    -S, --silent    Don't log anything to the console
    -s, --single    Serve single page applications (sets `-c` to 1 day)
    -t, --treeless  Don't display statics tree (disabled by default)
    -u, --unzipped  Disable GZIP compression
    -v, --version   Output the version number

If you need to watch for changes, see hostr, credit Henry Tseng's answer

查看更多
墨雨无痕
4楼-- · 2019-01-01 14:27

connect could be what you're looking for.

Installed easily with:

npm install connect

Then the most basic static file server could be written as:

var connect = require('connect'),
    directory = '/path/to/Folder';

connect()
    .use(connect.static(directory))
    .listen(80);

console.log('Listening on port 80.');
查看更多
孤独寂梦人
5楼-- · 2019-01-01 14:30

DEMO/PROTO SERVER ONLY

If that's all you need, try this:

const http = require('http');
const fs = require('fs');
const port = 3000;
const app = http.createServer((req,res) => {
    res.writeHead(200);
    res.end(fs.readFileSync(__dirname + req.url));
});

app.listen(port);

note: You need to use "/index.html" as part of your address ie "http://localhost:3000/index.html"

查看更多
呛了眼睛熬了心
6楼-- · 2019-01-01 14:30

If you are intrested in ultra-light http server without any prerequisites you should have a look at: mongoose

查看更多
旧时光的记忆
7楼-- · 2019-01-01 14:31

Here is my one-file/lightweight node.js static file web-server pet project with no-dependency that I believe is a quick and rich tool which its use is as easy as issuing this command on your Linux/Unix/macOS terminal (or termux on Android) when node.js (or nodejs-legacy on Debian/Ubuntu) is installed:

curl pad.js.org | node 

(different commands exist for Windows users on the documentation)

It supports different things that I believe can be found useful,

  • Hierarchical directory index creation/serving
    • With sort capability on the different criteria
    • Upload from browser by [multi-file] drag-and-drop and file/text-only copy-paste and system clipboard screen-shot paste on Chrome, Firefox and other browsers may with some limitations (which can be turned off by command line options it provides)
    • Folder/note-creation/upload button
  • Serving correct MIMEs for well known file types (with possibility for disabling that)
  • Possibility of installation as a npm package and local tool or, one-linear installation as a permanent service with Docker
  • HTTP 206 file serving (multipart file transfer) for faster transfers
  • Uploads from terminal and browser console (in fact it was originally intended to be a file-system proxy for JS console of browsers on other pages/domains)
  • CORS download/uploads (which also can be turned off)
  • Easy HTTPS integration
  • Lightweight command line options for achieving better secure serving with it:
    • With my patch on node.js 8, you can have access to the options without first installation: curl pad.js.org | node - -h
    • Or first install it as a system-global npm package by [sudo] npm install -g pad.js and then use its installed version to have access to its options: pad -h
    • Or use the provided Docker image which uses relatively secure options by default. [sudo] docker run --restart=always -v /files:/files --name pad.js -d -p 9090:9090 quay.io/ebraminio/pad.js

Screenshot of a folder index using the tool

The features described above are mostly documented on the main page of the tool http://pad.js.org which by some nice trick I used is also the place the tool source itself is also served from!

The tool source is on GitHub which welcomes your feedback, feature requests and ⭐s!

查看更多
登录 后发表回答