{"id":13528019,"url":"https://github.com/cloudhead/node-static","last_synced_at":"2025-05-13T23:03:59.753Z","repository":{"id":991890,"uuid":"799844","full_name":"cloudhead/node-static","owner":"cloudhead","description":"rfc 2616 compliant HTTP static-file server module, with built-in caching.","archived":false,"fork":false,"pushed_at":"2024-08-22T12:11:16.000Z","size":974,"stargazers_count":2171,"open_issues_count":39,"forks_count":245,"subscribers_count":51,"default_branch":"master","last_synced_at":"2025-05-08T03:03:10.333Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cloudhead.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGES.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2010-07-27T04:27:12.000Z","updated_at":"2025-04-06T04:27:42.000Z","dependencies_parsed_at":"2024-11-10T00:36:24.424Z","dependency_job_id":"0d69e3f6-4bb2-40a0-9e8e-13bfda1a99b0","html_url":"https://github.com/cloudhead/node-static","commit_stats":{"total_commits":253,"total_committers":54,"mean_commits":4.685185185185185,"dds":0.7035573122529644,"last_synced_commit":"bd48e282d831745d0721158d1752d31d8fa602a6"},"previous_names":[],"tags_count":19,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cloudhead%2Fnode-static","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cloudhead%2Fnode-static/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cloudhead%2Fnode-static/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cloudhead%2Fnode-static/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cloudhead","download_url":"https://codeload.github.com/cloudhead/node-static/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253471912,"owners_count":21913952,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T06:02:10.281Z","updated_at":"2025-05-13T23:03:59.709Z","avatar_url":"https://github.com/cloudhead.png","language":"JavaScript","readme":"# node-static\n\n[![Node.js CI status](https://github.com/http://github.com/cloudhead/node-static/workflows/Node.js%20CI/badge.svg)](https://github.com/http://github.com/cloudhead/node-static/actions)\n\n\u003e a simple, *rfc 2616 compliant* file streaming module for [node](http://nodejs.org)\n\nnode-static understands and supports *conditional GET* and *HEAD* requests.\nnode-static was inspired by some of the other static-file serving modules out\nthere, such as node-paperboy and antinode.\n\n## Installation\n\n```sh\n$ npm install node-static\n```\n\n## Set-up\n\n### ESM\n\n```js\nimport {Server, version, mime} from 'node-static';\n\n// OR:\n// import * as statik from 'node-static';\n```\n\n### CommonJS\n\n```js\nconst statik = require('node-static');\n```\n\n## Usage\n\n```js\n//\n// Create a node-static server instance to serve the './public' folder\n//\nconst file = new statik.Server('./public');\n\nrequire('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```\n\n## API\n\n### Creating a node-static Server\n\nCreating a file server instance is as simple as:\n\n```js\nnew statik.Server();\n```\n\nThis will serve files in the current directory. If you want to serve files in\na specific directory, pass it as the first argument:\n\n```js\nnew statik.Server('./public');\n```\n\nYou can also specify how long the client is supposed to cache the files\nnode-static serves:\n\n```js\nnew statik.Server('./public', { cache: 3600 });\n```\n\nThis will set the `Cache-Control` header, telling clients to cache the file for\nan hour. This 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`\ninstance, passing it the HTTP request and response object:\n\n```js\nconst statik = require('node-static');\n\nvar fileServer = new statik.Server('./public');\n\nrequire('http').createServer(function (request, response) {\n    request.addListener('end', function () {\n        fileServer.serve(request, response);\n    }).resume();\n}).listen(8080);\n```\n\n### Serving specific files\n\nIf you want to serve a specific file, like an error page for example, use the\n`serveFile` method:\n\n```js\nfileServer.serveFile('/error.html', 500, {}, request, response);\n```\n\nThis will serve the `error.html` file, from under the file root directory, with\na `500` status code.\nFor example, you could serve an error page, when the initial request wasn't\nfound:\n\n```js\nrequire('http').createServer(function (request, response) {\n    request.addListener('end', function () {\n        fileServer.serve(request, response, function (e, res) {\n            if (e \u0026\u0026 (e.status === 404)) { // If the file wasn't found\n                fileServer.serveFile(\n                    '/not-found.html', 404, {}, request, response\n                );\n            }\n        });\n    }).resume();\n}).listen(8080);\n```\n\nMore on intercepting errors bellow.\n\n### Intercepting errors \u0026 Listening\n\nAn optional callback can be passed as last argument, it will be called every\ntime a file has been served successfully, or if there was an error serving the\nfile:\n\n```js\nconst statik = require('node-static');\n\nconst fileServer = new statik.Server('./public');\n\nrequire('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                console.error(\n                    \"Error serving \" + request.url + \" - \" + err.message\n                );\n\n                // Respond to the client\n                response.writeHead(err.status, err.headers);\n                response.end();\n            }\n        });\n    }).resume();\n}).listen(8080);\n```\n\nNote that if you pass a callback, and there is an error serving the file,\nnode-static *will not* respond to the client. This gives you the opportunity\nto re-route the request, or handle it differently.\n\nFor example, you may want to interpret a request as a static request, but if\nthe file isn't found, send it to an application.\n\nIf you only want to *listen* for errors, you can use *event listeners*:\n\n```js\nfileServer.serve(request, response).addListener('error', function (err) {\n    console.error(\"Error serving \" + request.url + \" - \" + err.message);\n});\n```\n\nWith this method, you don't have to explicitly send the response back, in case\nof an error.\n\n### Options when creating an instance of `Server`\n\n#### `cache` (Default: `3600`)\n\nSets the `Cache-Control` header.\n\nexample: `{ cache: 7200 }` will set the max-age for all files to 7200 seconds\nexample: `{ cache: {'**/*.css': 300}}` will set the max-age for all CSS files to 5 minutes.\n\nPassing a number will set the cache duration to that number of seconds.\nPassing `false` will disable the `Cache-Control` header.\nPassing a object with [minimatch glob pattern](https://github.com/isaacs/minimatch)\nkeys and number values will set cache max-age for any matching paths.\n\n#### `serverInfo` (Default: `node-static/{version}`)\n\nSets the `Server` header.\n\nexample: `{ serverInfo: \"myserver\" }`\n\n#### `headers` (Default: `{}`)\n\nSets response headers.\n\nexample: `{ headers: { 'X-Hello': 'World!' } }`\n\n#### `gzip` (Default: `false`)\n\nEnable support for sending compressed responses.  This will enable a check for\na file with the same name plus '.gz' in the same folder.  If the compressed\nfile is found and the client has indicated support for gzip file transfer,\nthe contents of the .gz file will be sent in place of the uncompressed file\nalong with a Content-Encoding: gzip header to inform the client the data has\nbeen 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\nthe respond would match that RegExp using its test() method.\n\n#### `indexFile` (Default: `index.html`)\n\nChoose a custom index file when serving up directories.\n\nexample: `{ indexFile: \"index.htm\" }`\n\n#### `defaultExtension` (Default: `null`)\n\nChoose a default extension when serving files.\nA request to '/myFile' would check for a `myFile` folder (first) then a\n`myFile.html` (second).\n\nexample: `{ defaultExtension: \"html\" }`\n\n## Command Line Interface\n\n`node-static` also provides a CLI.\n\n```text\n--port, -p          TCP port at which the files will be served                        [default: 8080]\n--host-address, -a  the local network interface at which to listen                    [default: \"127.0.0.1\"]\n--cache, -c         \"Cache-Control\" header setting, defaults to 3600\n--version, -v       node-static version\n--headers, -H       additional headers (in JSON format)\n--header-file, -f   JSON file of additional headers\n--gzip, -z          enable compression (tries to serve file of same name plus '.gz')\n--spa               Serve the content as a single page app by redirecting all\n                    non-file requests to the index HTML file.\n--indexFile, -i     Specify a custom index file when serving up directories.          [default: \"index.html\"]\n--help, -h          display this help message\n```\n\n### Example Usage\n\n```sh\n# serve up the current directory\n$ static\nserving \".\" at http://127.0.0.1:8080\n\n# serve up a different directory\n$ static public\nserving \"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\"}'\nserving \".\" at http://127.0.0.1:8080\n\n# set cache control max age\n$ static -c 7200\nserving \".\" at http://127.0.0.1:8080\n\n# expose the server to your local network\n$ static -a 0.0.0.0\nserving \".\" at http://0.0.0.0:8080\n\n# show help message, including all options\n$ static -h\n```\n","funding_links":[],"categories":["Server Side","JavaScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcloudhead%2Fnode-static","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcloudhead%2Fnode-static","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcloudhead%2Fnode-static/lists"}