Git Product home page Git Product logo

node-static-http-servers's Introduction

Node Static HTTP Servers

Build Status Dependencies Status Known Vulnerabilities License

Examples of serving static files with HTTP using Node.js on five levels of abstraction - from express.static to directly using the net module with raw TCP sockets.

It was written for the answer on Stack Overflow:

Read this answer for some background and better explanation.

Other related answers on Stack Overflow:

Examples

Every example serves the same files from the public directory and supports the minumum functionality of:

  • MIME types for most common files
  • must serve HTML, JS, CSS, plain text and images
  • serves index.html as a default directory index
  • responds with error codes for missing files
  • no path traversal vulnerabilities
  • no race conditions while reading files

Every version is tested with Travis on Node versions 4, 5, 6 and 7. See test results.

All examples:

  1. express.static
  2. express
  3. connect
  4. http
  5. net

express.static

estatic.js (raw)

This version uses the express.static built-in middleware of the express module.

This example has the most functionality and the least amount of code.

express

express.js (raw)

This version uses the express module but without the express.static middleware. Serving static files is implemented as a single route handler using streams.

This example has simple path traversal countermeasures and supports a limited set of most common MIME types.

connect

connect.js (raw)

This version uses the connect module which is a one level of abstraction lower than express.

This example has similar functionality to the express version but using slightly lower-lever APIs.

http

http.js (raw)

This version uses the http module which is the lowest-level API for HTTP in Node.

This example has similar functionality to the connect version but using even more lower-level APIs.

net

net.js (raw)

This version uses the net module which is the lowest-level API for TCP sockets in Node.

This example has some of the functionality of the http version but the minimal and incomplete HTTP protocol has been implemented from scratch. Since it doesn't support chunked encoding it loads the files into memory before serving them to know the size before sending a response because statting the files and then loading would introduce a race condition.

Installation

Download the files using git:

git clone [email protected]:rsp/node-static-http-servers.git
# or:
git clone https://github.com/rsp/node-static-http-servers.git

Or download a ZIP file:

wget https://github.com/rsp/node-static-http-servers/archive/master.zip
tar xzvf master.zip

Install dependencies:

npm install

Running tests:

npm test

Running individual servers:

node net.js
node http.js
node connect.js
node express.js
node estatic.js

Issues

For any bug reports or feature requests please post an issue on GitHub.

Author

Rafał Pocztarski
Follow on GitHub Follow on Twitter
Follow on Stack Exchange

License

MIT License (Expat). See LICENSE.md for details.

node-static-http-servers's People

Contributors

rsp avatar

Watchers

James Cloos avatar Yaron Pdut avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.