A simple static server for development mode using Express JS
Note:
- This utility's purpose is to be used in your local development environments. So some common production standards are ignored.
- By default site-index, cors, compression (gzip) is enabled
npm install --global dev-express-static-server
dev-express-static-server <options>
--port=<port>
( default =8080
)--dir=<path-to-directory-to-server>
( default ={CWD}
)--https=<true/false>
[optional] ( default =false
)- You can also just specify
--https
to set it astrue
- You can also just specify
- If
--https
enabled,--keyPath=<path-to-key.pem-file>
[optional] ( default = "{CWD}/key.pem" ) - If
--https
enabled,--certPath=<path-to-cert.pem-file>
[optional] ( default = "{CWD}/cert.pem" )
E.g.
dev-express-static-server --port=9092 --https --keyPath="~/dev-certs/key.pem" --certPath="~/dev-certs/cert.pem"
Note:
{CWD}
is current directory where the command/util is running from- All paths above support HOME dir shortcut char
~
- Temporary PID files will be managed by the script on the current dir to manage servers, if started in background.
dev-express-static-server --stop --port=<port>
E.g.
dev-express-static-server --stop --port=9092
Note:
--port
option is mandatory as PID files will be created using port numbers as ID and using that we will shutdown node process.
let server = require("dev-express-static-server/server.js");
-
port
is required argument -
directory
- Directory to serve ( default ={CWD}
) -
opts
opts.useHttps
- Iftrue
,https
server instance will be created, otherwisehttp
will be usedopts.keyPath
- Ifhttps
enabled, path tokey.pem
file ( default ={CWD}/key.pem
)opts.certPath
- Ifhttps
enabled, path tocert.pem
file ( default ={CWD}/cert.pem
)opts.onListen
-[callback]
function which will be invoked once the server starts up successfully and is listening for clients. An object argument with{ pid: process.pid }
will be passed to the callback.
opts = { onListen: function (args) { console.log(args.pid); } }
opts.onError
-[callback]
function which will be invoked in case any error happens in server startup. The thrownerror
will be passed to the callback.
opts = { onError: function (err) { console.log(err.message); } }
opts.onClose
-[callback]
function which will be invoked once the server is stopped.
opts = { onClose: function () { console.log("Cleanup !"); } }
- Returns an object with
close
method, which if called shuts down the server
let serverInstance = server.start(9092, path.resolve("../app/", {
https: true,
keyPath: path.join("../../dev-certs/key.pem"),
certPath: path.join("../../dev-certs/cert.pem"),
onListen: ...,
onClose: ...,
onError: ...
}) );
// After job done ..
serverInstance.close();
You can also kill your server if running in a separate processs and you know its process ID.
Note:
- Be careful to not kill your current process
processID
is required argument
E.g.
server.kill(9879869);