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

前端 未结 30 1792
攒了一身酷
攒了一身酷 2020-11-22 12:30

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



        
相关标签:
30条回答
  • 2020-11-22 12:56

    You can use the NPM serve package for this, if you don't need the NodeJS stuff it is a quick and easy to use tool:

    1 - Install the package on your PC:

    npm install -g serve
    

    2 - Serve your static folder with serve <path> :

    d:> serve d:\StaticSite
    

    It will show you which port your static folder is being served, just navigate to the host like:

    http://localhost:3000
    
    0 讨论(0)
  • 2020-11-22 12:56

    Searching in NPM registry https://npmjs.org/search?q=server, I have found static-server https://github.com/maelstrom/static-server

    Ever needed to send a colleague a file, but can't be bothered emailing the 100MB beast? Wanted to run a simple example JavaScript application, but had problems with running it through the file:/// protocol? Wanted to share your media directory at a LAN without setting up Samba, or FTP, or anything else requiring you to edit configuration files? Then this file server will make your life that little bit easier.

    To install the simple static stuff server, use npm:

    npm install -g static-server
    

    Then to serve a file or a directory, simply run

    $ serve path/to/stuff
    Serving path/to/stuff on port 8001
    

    That could even list folder content.

    Unfortunately, it couldn't serve files :)

    0 讨论(0)
  • 2020-11-22 12:57

    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:

    var express = require('express');
    var server = express();
    server.use('/', express.static(__dirname + '/'));
    server.listen(8080);
    

    If you wish to put it in a different location, set the path on the third line:

    server.use('/', express.static(__dirname + '/public'));
    

    CD to the folder containing your file and run node from the console with this command:

    node server.js
    

    Browse to localhost:8080

    0 讨论(0)
  • 2020-11-22 12:59

    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

    0 讨论(0)
  • 2020-11-22 13:01

    For people wanting a server runnable from within NodeJS script:

    You can use expressjs/serve-static which replaces connect.static (which is no longer available as of connect 3):

    myapp.js:

    var http = require('http');
    
    var finalhandler = require('finalhandler');
    var serveStatic = require('serve-static');
    
    var serve = serveStatic("./");
    
    var server = http.createServer(function(req, res) {
      var done = finalhandler(req, res);
      serve(req, res, done);
    });
    
    server.listen(8000);
    

    and then from command line:

    • $ npm install finalhandler serve-static
    • $ node myapp.js
    0 讨论(0)
  • 2020-11-22 13:02

    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!

    0 讨论(0)
提交回复
热议问题