{ "name": "node-static", "description": "simple, compliant file streaming module for node", "url": "http://github.com/cloudhead/node-static", "keywords": [ "http", "static", "file", "server" ], "author": { "name": "Alexis Sellier", "email": "self@cloudhead.net" }, "contributors": [ { "name": "Pablo Cantero", "email": "pablo@pablocantero.com" } ], "repository": { "type": "git", "url": "http://github.com/cloudhead/node-static" }, "main": "./lib/node-static", "scripts": { "test": "vows --spec --isolate" }, "bin": { "static": "bin/cli.js" }, "license": "MIT", "dependencies": { "optimist": ">=0.3.4", "colors": ">=0.6.0", "mime": ">=1.2.9" }, "devDependencies": { "request": "latest", "vows": "latest" }, "version": "0.7.3", "engines": { "node": ">= 0.4.1" }, "readme": "node-static\n===========\n\n> a simple, *rfc 2616 compliant* file streaming module for [node](http://nodejs.org)\n\nnode-static has an in-memory file cache, making it highly efficient.\nnode-static understands and supports *conditional GET* and *HEAD* requests.\nnode-static was inspired by some of the other static-file serving modules out there,\nsuch as node-paperboy and antinode.\n\nSynopsis\n--------\n\n var static = require('node-static');\n\n //\n // Create a node-static server instance to serve the './public' folder\n //\n var file = new static.Server('./public');\n\n require('http').createServer(function (request, response) {\n request.addListener('end', function () {\n //\n // Serve files!\n //\n file.serve(request, response);\n }).resume();\n }).listen(8080);\n\nAPI\n---\n\n### Creating a node-static Server #\n\nCreating a file server instance is as simple as:\n\n new static.Server();\n\nThis will serve files in the current directory. If you want to serve files in a specific\ndirectory, pass it as the first argument:\n\n new static.Server('./public');\n\nYou can also specify how long the client is supposed to cache the files node-static serves:\n\n new static.Server('./public', { cache: 3600 });\n\nThis will set the `Cache-Control` header, telling clients to cache the file for an hour.\nThis is the default setting.\n\n### Serving files under a directory #\n\nTo serve files under a directory, simply call the `serve` method on a `Server` instance, passing it\nthe HTTP request and response object:\n \n var static = require('node-static');\n\n var fileServer = new static.Server('./public');\n\n require('http').createServer(function (request, response) {\n request.addListener('end', function () {\n fileServer.serve(request, response);\n }).resume();\n }).listen(8080);\n\n### Serving specific files #\n\nIf you want to serve a specific file, like an error page for example, use the `serveFile` method:\n\n fileServer.serveFile('/error.html', 500, {}, request, response);\n\nThis will serve the `error.html` file, from under the file root directory, with a `500` status code.\nFor example, you could serve an error page, when the initial request wasn't found:\n\n require('http').createServer(function (request, response) {\n request.addListener('end', function () {\n fileServer.serve(request, response, function (e, res) {\n if (e && (e.status === 404)) { // If the file wasn't found\n fileServer.serveFile('/not-found.html', 404, {}, request, response);\n }\n });\n }).resume();\n }).listen(8080);\n\nMore on intercepting errors bellow.\n\n### Intercepting errors & Listening #\n\nAn optional callback can be passed as last argument, it will be called every time a file\nhas been served successfully, or if there was an error serving the file:\n\n var static = require('node-static');\n \n var fileServer = new static.Server('./public');\n\n require('http').createServer(function (request, response) {\n request.addListener('end', function () {\n fileServer.serve(request, response, function (err, result) {\n if (err) { // There was an error serving the file\n sys.error(\"Error serving \" + request.url + \" - \" + err.message);\n\n // Respond to the client\n response.writeHead(err.status, err.headers);\n response.end();\n }\n });\n }).resume();\n }).listen(8080);\n\nNote that if you pass a callback, and there is an error serving the file, node-static\n*will not* respond to the client. This gives you the opportunity to re-route the request,\nor handle it differently.\n\nFor example, you may want to interpret a request as a static request, but if the file isn't found,\nsend it to an application.\n\nIf you only want to *listen* for errors, you can use *event listeners*:\n\n fileServer.serve(request, response).addListener('error', function (err) {\n sys.error(\"Error serving \" + request.url + \" - \" + err.message);\n });\n\nWith this method, you don't have to explicitly send the response back, in case of an error.\n\n### Options when creating an instance of `Server` #\n\n#### `cache` #\n\nSets the `Cache-Control` header.\n\nexample: `{ cache: 7200 }`\n\nPassing a number will set the cache duration to that number of seconds.\nPassing `false` will disable the `Cache-Control` header.\n\n> Defaults to `3600`\n\n\n#### `serverInfo` #\n\nSets the `Server` header.\n\nexample: `{ serverInfo: \"myserver\" }`\n\n> Defaults to `node-static/{version}`\n\n#### `headers` #\n\nSets response headers.\n\nexample: `{ 'X-Hello': 'World!' }`\n\n> defaults to `{}`\n\n#### `gzip` #\n\nEnable support for sending compressed responses. This will enable a check for a\nfile with the same name plus '.gz' in the same folder. If the compressed file is\nfound and the client has indicated support for gzip file transfer, the contents\nof the .gz file will be sent in place of the uncompressed file along with a\nContent-Encoding: gzip header to inform the client the data has been compressed.\n\nexample: `{ gzip: true }`\nexample: `{ gzip: /^\\/text/ }`\n\nPassing `true` will enable this check for all files.\nPassing a RegExp instance will only enable this check if the content-type of the\nrespond would match that RegExp using its test() method.\n\n> Defaults to `false`\n\n\nCommand Line Interface\n----------------------\n\n`node-static` also provides a CLI.\n\n### Installation #\n\n $ npm install -g node-static\n\n### Example Usage #\n\n # serve up the current directory\n $ static\n serving \".\" at http://127.0.0.1:8080\n\n # serve up a different directory\n $ static public\n serving \"public\" at http://127.0.0.1:8080\n\n # specify additional headers (this one is useful for development)\n $ static -H '{\"Cache-Control\": \"no-cache, must-revalidate\"}'\n serving \".\" at http://127.0.0.1:8080\n\n # set cache control max age\n $ static -c 7200\n serving \".\" at http://127.0.0.1:8080\n\n # show help message, including all options\n $ static -h\n", "readmeFilename": "README.md", "bugs": { "url": "https://github.com/cloudhead/node-static/issues" }, "_id": "node-static@0.7.3", "dist": { "shasum": "87eff7b09dbc5f150aba7161d839ded50f52a05d" }, "_from": "node-static@", "_resolved": "https://registry.npmjs.org/node-static/-/node-static-0.7.3.tgz" }