Simple fast and static node.js server
$ npm install -g node-srv
# Start server on port 8000 in current dir
$ node-srv
# Start server on port 8000 in parent dir
$ node-srv ..
# Start server on port 8001 writing logs to *./nodeserver.log* file
$ node-srv --port 8001 --logs ./nodeserver.log
new Server(options, routes, handlers, exitCallback);
// Require module
var server = require('node-srv');
// Start server
var srv = new Server({
port: 5000,
root: '../www/',
logs: true
});
// Update server port (automatically restert server with new port)
srv.options.port = 5001;
// Stop server
srv.stop();
- -p, --port [number],
port
— Port the server is started on (default8000
, or env PORT) - -h, --host [host],
host
— Host or ip address on which the server will work (any host0.0.0.0
by default) - -i, --index [file],
index
— Sets default index file for directories. For example: for uri/test/
, server opentest/index.html
. Defaultindex.html
- -l, --logs [path/boolean],
logs
— Write logs flag. If you specify a path, it will write to that file (if path is folder, default filename will be node-srv.log). Defaultfalse
- -t, --timeout [ms],
timeout
— Requset timeout (in ms). Default30000
- -s, --https [boolean],
https
— Force create HTTPS server (only with--key
and--cert
options). Defaultfalse
- --key [path],
key
— Path to key file for https server - --cert [path],
cert
— Path to certificate file for https server - --cors [hosts],
cors
— Enable CORS. If empty uses*
for host. Defaultfalse
- --not-found [path],
notFound
— Path to 404 error page. Defaultnull
- --help — print help
- --version — print version
Usage as Grunt.js task
- Install node-srv locally
$ npm i node-srv
- Load task into your Gruntfile
grunt.loadTasks('node-srv');
- Configure as multitask
grunt.initConfig({
srv: {
server1: {
port: 4001,
'404': './404.html'
index: 'index.htm',
keepalive: false
},
server2: {
port: 4002,
logs: true
},
}
});
- Run task
$ grunt srv:server2
You can extend server class.
const Server = require('node-srv');
class MyServer extends Server {
log(string) {
console.log(string);
}
}
You can add custom handlres specific path patterns (like minimatch).
Parameters way:
const Server = require('node-srv');
new Server({
// options
port: 8000
}, {
// routes
'**/*.md': 'markdown', // handler name for handlers list
'_healthcheck': (params, resolve) => { // direct handler function
resolve({
body: `OK: ${params.method} ${params.uri}`, // "OK: GET /_healthcheck"
code: 200,
headers: {'Content-Type': 'text/plain'}
});
}
}, {
markdown: (params, resolve, reject) => { // handlers key-value list
markdown.renderFile(params.file).then( html => {
resolve({
body: html,
code: 200,
headers: {'Content-Type': 'text/html'}
}, (error) => {
if (error.code === 'ENOENT') {
reject({handler: 'notFound'});
} else {
reject({error});
}
});
});
}
});
Extend way:
const Server = require('node-srv');
class MyServer extends Server {
routes() {
return {
'**/*.md': 'markdown',
'_healthcheck': (params, resolve) => {
... // as in parameters
}
};
}
handlers() {
return {
markdown: (params, resolve, reject) => {
... // as in parameters
}
}
}
}
new MyServer();
You can return HTTP code or Promise object (and resolve HTTP code).
Default handlers:
- file — response file
- notFound — response error 404 page (default or optional)
- timeout — response timeout page (by default on request timeout)
- serverError — response error 500 page. Define error code by
reject({code: 403})
and page will return that. - options — response for OPTIONS request method (CORS)
You can override its with any way.
CLI options:
- -r, --root removed. Use arguments: old
node-srv --root ../web
, newnode-srv ../web
- --404 renamed to --not-found
- -k shortcut removed from --key. Use only full flag
- -c shortcut removed from --cert. Use only full flag
Program API:
- class arguments changed
- handlers architecture changed