node-static-serv is a small configurable static server written on pure node.js. It allows you caching http responses, content encoding, CORS and simple logging to console or file. You may use it from your back-end code with multiple instances or serve static files with cli command. Simple example:
var Server = require('node-static-serv');
var options = { root: './public', cache: true, encoding: true, port: 3000}
var server = new Server(options);
server.start();Create new instance of node-static-servwith the given options. All options are optional.
options <Object>cache <Boolean>- enables caching mechanism. Allows to use conditionalGETsemantics for request messages, which includesIf-Modified-Sincefield by providingLast-Modifiedvalue. Also usesCache-Controlheader to provide explicit directives to the HTTP caches. By default this field value set tofalsecacheHeaders <String>- value ofCache-Controlheader. Bu default set toprivate, max-age=3600, must-revalidateencoding <Boolean>- allows documents to be compressed without loss of information. Server fully implements an RFC-2616 algorithm to select most preferred content-coding from client request header fieldAccept-Encoding. Also it uses local in-memory cache to store already compressed documents, which reduces cpu-intensive compression algorithms calls. This value isfalseby defaultcors <Boolean>enables cross-origin requests.falseby defaulthost <String>by default set to127.0.0.1port <Number>by default set to3000root <String>path to directory to serve. By default set to./name <String>value of ````X-Powered-Byheader. Set tonode.js``` by defaultlogToFile <Boolean>enable logging to file. IflogFileoptions field not specified, shold log to file with defalut pathlogFile <String>path to log file. By defalut set tosyslog.filelogToConsole <Boolean>enable console loggingnotFound <String>path to standart '404 Not Found' html file. If not specified, server should respond with empty body and404code
callback <Function>Start server instance and call the given callback.
callback <Function>Stop the server and call the given callback.
In order to run node-static-serv from console, use command with next syntax:
nss [path_to_config] | [path_to_root]
[path_to_config]- is the path to yourconfig.jsfile, which determines same options, as you may provide to configure server from JavaScript code.
// config.js
module.exports = { root: './root', cache: true, gzip: true, port: 3000}[path-to-root]- path to your root folder, which static server should serve. Simple example:
nss ../myconf.jsIn this case, you provide all nesessary data for server in ./myconf.js file. If no root field specified, the default root path should used (./)
nss ./publicIn this case, you provide only the path to your public directory which you want node-static-server to serve. Default server configurations should be applied.
nssHere node-static-server should run static server with fully default configs.
