Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
1a77010
Respect static --cache 0
matthew-andrews Oct 26, 2014
9cfcc55
make sure ip/localhost/127.0.0.1 all work
flyingsky Nov 10, 2014
78cd2cb
fix bug, cannot access with local ip #139
flyingsky Nov 10, 2014
6617149
Merge branch 'master' of https://github.com/flyingsky/node-static
flyingsky Nov 10, 2014
b7ca339
Adding travis
fmalk Nov 9, 2015
33211f4
Trying to finish the run
fmalk Nov 9, 2015
73bcc93
Test bugfix
fmalk Nov 9, 2015
d3552ad
New fileServer option: defaultExtension
fmalk Nov 10, 2015
bd637f3
Test validation of new option "defaultExtension"
fmalk Nov 10, 2015
f086f9a
Test added: default extension does not interfere with folders
fmalk Nov 10, 2015
bd5d044
Small fix
fmalk Nov 10, 2015
6ccc79a
Added glob matching feature for setting cache headers.
lightswitch05 Sep 27, 2016
47fe715
Fix vulnerabilities found with npm audit
fidian Feb 4, 2021
9c9389b
Protect fs.stats calls from bad path arguments
brpvieira Mar 1, 2021
03be38d
- Optimization: 'use strict' directive (and avoid octal)
brettz9 Mar 28, 2021
e18ca01
- Time display logging with leading 0
brettz9 Mar 28, 2021
036c6f7
- npm: Add scoping: @brettz9/node-static
brettz9 Mar 28, 2021
1e7db15
- Docs: Fix header example
brettz9 Mar 28, 2021
c1eb9d6
Support bytes=0-0 range header
brettz9 Mar 29, 2021
e19686f
For spa, allow dots after path
brettz9 Mar 29, 2021
48691d2
- Enhancement: Allow `serverInfo` to be `null`; add test for null and…
brettz9 Mar 29, 2021
83aac2e
- Prefer includes or startsWith over indexOf where possible
brettz9 Mar 29, 2021
78879dc
Merge commit '9c9389b30caa43c3e2c6f64d5adcad47780b2cde'
brettz9 Mar 29, 2021
3909741
- Docs: CHANGES.md
brettz9 Mar 29, 2021
c7be162
Respect static --cache 0
brettz9 Mar 29, 2021
98d490e
- Docs: Fix typo
brettz9 Mar 29, 2021
798e5a3
- git: Ignore `.idea`
brettz9 Mar 29, 2021
1204501
Merge commit '6617149b4c9f8c30251f5adceba0942c5ff2d097'
brettz9 Mar 29, 2021
0673494
- Add another fs.stat guard
brettz9 Mar 29, 2021
1f91054
Merge commit 'bd5d0444b2ef0d9709920f87e34f0a02c6411bdc'
brettz9 Mar 29, 2021
d298891
- npm: Update lock file
brettz9 Mar 29, 2021
eeae450
- Optimization: Remove unused `colors`
brettz9 Mar 29, 2021
5a2873b
- npm: Update `mime` (updating to latest minor update only)
brettz9 Mar 29, 2021
f7de620
Merge commit '47fe715364acd79f4bdd2df54b14d94e1e8f7009'
brettz9 Mar 29, 2021
69d9f54
- Refactoring: Use safer non-prototype version of `colors` (had indee…
brettz9 Mar 29, 2021
4573433
- Fix bugs/homepage URL
brettz9 Mar 29, 2021
3d70b2e
- Docs: Add missing credit to CHANGES
brettz9 Mar 29, 2021
8e3059e
- Docs: Code
brettz9 Mar 29, 2021
26a4874
- Testing: Add checks for supposed direct node-static unauthorized fi…
brettz9 Mar 29, 2021
9548717
- Use non-deprecated URL constructor for parsing; also appears it may…
brettz9 Mar 29, 2021
e53e174
Merge commit '6ccc79a44c57eea79186521be72b90b7b1fefd57'
brettz9 Mar 29, 2021
490b861
- Testing: Add `example.com` file to confirm https://www.npmjs.com/ad…
brettz9 Mar 29, 2021
f09dd82
- Add DOCTYPE in fixtures (for suppressing IDE validation)
brettz9 Mar 29, 2021
c4009f6
- Docs: Give credit in CHANGES
brettz9 Mar 29, 2021
f876086
- Docs: Set off code
brettz9 Mar 29, 2021
cdeb737
- npm: Update `minimatch`
brettz9 Mar 29, 2021
f0794df
- Remove ncurc file (already updated)
brettz9 Mar 29, 2021
94045ce
- License: Add own name
brettz9 Mar 29, 2021
8135831
- Update/fix: Protect additional `fs.stat` call (for `defaultExtension`)
brettz9 Mar 29, 2021
07534e7
- Docs: Detail some changes from fork
brettz9 Mar 30, 2021
dc5ce7e
- Docs: CHANGES clarifications
brettz9 Mar 30, 2021
03c87d2
- Docs (CHANGES): Denote security fix
brettz9 Mar 30, 2021
0392e64
- Docs (CHANGES): Further security fix denotations
brettz9 Mar 30, 2021
cab19a6
- Docs (CHANGES): Fix labeling of issue and indicate security fixes a…
brettz9 Mar 30, 2021
97d7ce7
- Testing: Add `nyc` for coverage
brettz9 Mar 30, 2021
6fa3ace
- Docs (CHANGES): Fix
brettz9 Mar 30, 2021
c2033de
Revert scoping changes; line breaks; use of `const` in README
brettz9 May 21, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
; EditorConfig file: https://EditorConfig.org
; Install the "EditorConfig" plugin into your editor to use

root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 4
trim_trailing_whitespace = true

[*.json]
indent_size = 2
15 changes: 15 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

module.exports = {
env: {
es6: true,
node: true
},
extends: ['eslint:recommended'],
rules: {
indent: ['error', 4],
'no-var': ['error'],
'no-unused-vars': ['error', {args: 'none'}],
'prefer-const': ['error']
}
};
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
npm-debug.log
node_modules

.idea
2 changes: 2 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
test
benchmark
46 changes: 46 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# CHANGES for `node-static`

## ? (UNRELEASED)

### User-facing

- **Breaking change** (npm): Set `engines` to 10.11.0+
- Security: Fix dependency vulnerabilities by switching from `optimist` to
`neodoc` (@fidian)
- Security: Update `mime` and `colors` (@fidian)
- Security Update/fix: Use `URL` constructor over deprecated `url.parse`;
should fix Open Redirect issue <https://www.npmjs.com/advisories/1207>
- Security Update/fix: Protect `fs.stat` calls from bad path arguments; fixes
Denial of Service issue <https://www.npmjs.com/advisories/1208>
(@brpvieira)
- Security fix?: The Unauthorized File Access issue
<https://www.npmjs.com/advisories/1206> does not appear to be an issue
per testing (if it ever was); if you can provide a test case where it
fails, please report
- Fix: Support `bytes=0-0` Range header (@prajwalkman)
- Fix: Avoid octal (@bgao / @Ilrilan)
- Fix: For `spa`, allow dots after path (@gjuchault)
- Enhancement: Allow access with local ip (@flyingsky)
- Enhancement: Allow `serverInfo` to be `null` (@martindale)
- Enhancement: Time display logging with leading 0 (@mauris)
- Enhancement: Respect static `--cache 0` (@matthew-andrews)
- Enhancement: New option: `defaultExtension` (@fmalk)
- Enhancement: Added glob matching for setting cache headers (@lightswitch05)
- Optimization: 'use strict' directive
- Docs: For examples (and internally) avoid `static` reserved word
- Docs: Fix header example (@emmanouil)
- Docs: Sp. (@EdwardBetts)
- Docs: Add `CHANGES.md`

### Dev-facing

- Linting: Prefer const, no-var, fix indent, comment-out unused,
prefer `startsWith` and `includes`
- Refactoring: Use safer non-prototype version of `colors`
- Maintenance: Add `.editorconfig`
- Testing: Add checks for supposed direct `node-static` vulnerabilities
- Testing: Add test for `null` and non-`null` serverInfo
- Testing: Allow tests to end (@fmalk)
- Testing: Add `nyc` for coverage
- npm: Add eslint devDep. and script
- npm: Add lock file
1 change: 1 addition & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Copyright (c) 2010-14 Alexis Sellier
Copyright (c) 2021 Brett Zamir

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
Expand Down
138 changes: 80 additions & 58 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
node-static
===========
# node-static

> a simple, *rfc 2616 compliant* file streaming module for [node](http://nodejs.org)

node-static understands and supports *conditional GET* and *HEAD* requests.
node-static was inspired by some of the other static-file serving modules out there,
such as node-paperboy and antinode.
node-static was inspired by some of the other static-file serving modules out
there, such as node-paperboy and antinode.

Synopsis
--------
# Synopsis

```js
var static = require('node-static');
const statik = require('node-static');

//
// Create a node-static server instance to serve the './public' folder
//
var file = new static.Server('./public');
const file = new statik.Server('./public');

require('http').createServer(function (request, response) {
request.addListener('end', function () {
Expand All @@ -31,39 +29,40 @@ require('http').createServer(function (request, response) {
API
---

### Creating a node-static Server #
### Creating a node-static Server

Creating a file server instance is as simple as:

```js
new static.Server();
new statik.Server();
```

This will serve files in the current directory. If you want to serve files in a specific
directory, pass it as the first argument:
This will serve files in the current directory. If you want to serve files in
a specific directory, pass it as the first argument:

```js
new static.Server('./public');
new statik.Server('./public');
```

You can also specify how long the client is supposed to cache the files node-static serves:
You can also specify how long the client is supposed to cache the files
node-static serves:

```js
new static.Server('./public', { cache: 3600 });
new statik.Server('./public', { cache: 3600 });
```

This will set the `Cache-Control` header, telling clients to cache the file for an hour.
This is the default setting.
This will set the `Cache-Control` header, telling clients to cache the file for
an hour. This is the default setting.

### Serving files under a directory #
### Serving files under a directory

To serve files under a directory, simply call the `serve` method on a `Server` instance, passing it
the HTTP request and response object:
To serve files under a directory, simply call the `serve` method on a `Server`
instance, passing it the HTTP request and response object:

```js
var static = require('node-static');
```js
const statik = require('node-static');

var fileServer = new static.Server('./public');
var fileServer = new statik.Server('./public');

require('http').createServer(function (request, response) {
request.addListener('end', function () {
Expand All @@ -72,23 +71,28 @@ require('http').createServer(function (request, response) {
}).listen(8080);
```

### Serving specific files #
### Serving specific files

If you want to serve a specific file, like an error page for example, use the `serveFile` method:
If you want to serve a specific file, like an error page for example, use the
`serveFile` method:

```js
fileServer.serveFile('/error.html', 500, {}, request, response);
```

This will serve the `error.html` file, from under the file root directory, with a `500` status code.
For example, you could serve an error page, when the initial request wasn't found:
This will serve the `error.html` file, from under the file root directory, with
a `500` status code.
For example, you could serve an error page, when the initial request wasn't
found:

```js
require('http').createServer(function (request, response) {
request.addListener('end', function () {
fileServer.serve(request, response, function (e, res) {
if (e && (e.status === 404)) { // If the file wasn't found
fileServer.serveFile('/not-found.html', 404, {}, request, response);
fileServer.serveFile(
'/not-found.html', 404, {}, request, response
);
}
});
}).resume();
Expand All @@ -97,21 +101,24 @@ require('http').createServer(function (request, response) {

More on intercepting errors bellow.

### Intercepting errors & Listening #
### Intercepting errors & Listening

An optional callback can be passed as last argument, it will be called every time a file
has been served successfully, or if there was an error serving the file:
An optional callback can be passed as last argument, it will be called every
time a file has been served successfully, or if there was an error serving the
file:

```js
var static = require('node-static');
var fileServer = new static.Server('./public');
const statik = require('node-static');

const fileServer = new statik.Server('./public');

require('http').createServer(function (request, response) {
request.addListener('end', function () {
fileServer.serve(request, response, function (err, result) {
if (err) { // There was an error serving the file
console.error("Error serving " + request.url + " - " + err.message);
console.error(
"Error serving " + request.url + " - " + err.message
);

// Respond to the client
response.writeHead(err.status, err.headers);
Expand All @@ -122,12 +129,12 @@ require('http').createServer(function (request, response) {
}).listen(8080);
```

Note that if you pass a callback, and there is an error serving the file, node-static
*will not* respond to the client. This gives you the opportunity to re-route the request,
or handle it differently.
Note that if you pass a callback, and there is an error serving the file,
node-static *will not* respond to the client. This gives you the opportunity
to re-route the request, or handle it differently.

For example, you may want to interpret a request as a static request, but if the file isn't found,
send it to an application.
For example, you may want to interpret a request as a static request, but if
the file isn't found, send it to an application.

If you only want to *listen* for errors, you can use *event listeners*:

Expand All @@ -137,76 +144,91 @@ fileServer.serve(request, response).addListener('error', function (err) {
});
```

With this method, you don't have to explicitly send the response back, in case of an error.
With this method, you don't have to explicitly send the response back, in case
of an error.

### Options when creating an instance of `Server` #
### Options when creating an instance of `Server`

#### `cache` #
#### `cache`

Sets the `Cache-Control` header.

example: `{ cache: 7200 }`
example: `{ cache: 7200 }` will set the max-age for all files to 7200 seconds
example: `{ cache: {'**/*.css': 300}}` will set the max-age for all CSS files to 5 minutes.

Passing a number will set the cache duration to that number of seconds.
Passing `false` will disable the `Cache-Control` header.
Passing a object with [minimatch glob pattern](https://github.com/isaacs/minimatch)
keys and number values will set cache max-age for any matching paths.

> Defaults to `3600`


#### `serverInfo` #
#### `serverInfo`

Sets the `Server` header.

example: `{ serverInfo: "myserver" }`

> Defaults to `node-static/{version}`

#### `headers` #
#### `headers`

Sets response headers.

example: `{ 'X-Hello': 'World!' }`
example: `{ headers: { 'X-Hello': 'World!' } }`

> defaults to `{}`

#### `gzip` #
#### `gzip`

Enable support for sending compressed responses. This will enable a check for a
file with the same name plus '.gz' in the same folder. If the compressed file is
found and the client has indicated support for gzip file transfer, the contents
of the .gz file will be sent in place of the uncompressed file along with a
Content-Encoding: gzip header to inform the client the data has been compressed.
Enable support for sending compressed responses. This will enable a check for
a file with the same name plus '.gz' in the same folder. If the compressed
file is found and the client has indicated support for gzip file transfer,
the contents of the .gz file will be sent in place of the uncompressed file
along with a Content-Encoding: gzip header to inform the client the data has
been compressed.

example: `{ gzip: true }`
example: `{ gzip: /^\/text/ }`

Passing `true` will enable this check for all files.
Passing a RegExp instance will only enable this check if the content-type of the
respond would match that RegExp using its test() method.
Passing a RegExp instance will only enable this check if the content-type of
the respond would match that RegExp using its test() method.

> Defaults to `false`

#### `indexFile` #
#### `indexFile`

Choose a custom index file when serving up directories.

example: `{ indexFile: "index.htm" }`

> Defaults to `index.html`

#### `defaultExtension`

Choose a default extension when serving files.
A request to '/myFile' would check for a `myFile` folder (first) then a
`myFile.html` (second).

example: `{ defaultExtension: "html" }`

> Defaults to `null`


Command Line Interface
----------------------

`node-static` also provides a CLI.

### Installation #
### Installation

```sh
$ npm install -g node-static
```

### Example Usage #
### Example Usage

```sh
# serve up the current directory
Expand Down
Loading