Express factory
Easy setup an Express instance
Installation
$ npm install express-factory --save
Prerequisite
Due to lack of dependency with express in this project, you have to install yourself express module in your project, this let you choose the version you want :
$ npm install express --save
Getting started
Run an express server with one line :
start; 'http://localhost:3000/';
Result :
How it works
express-factory creates instances of express servers (Express class), with following features :
- init method : initialize express configuration and components like middlewares, routers, routes
- arguments : optional config
- result : return this for chaining
- custom configuration passed through constructor, init or start method
- start method : initialize if needed and start the server
- arguments : optional config, and callback
- result : callback is called or returns a promise
- stop method : stop the server
- result : callback is called or returns a promise
Use cases
sample1.js
Simple express server :var expressFactory = request = expressInstance; expressInstance = ; expressInstancestart { request;};
Result :
$ node samples/sample1INFO - express-factory:125 - 214ms - server is listening on localhost:3000HTTP - logger:64 - 21ms - 127.0.0.1 - GET / - 200 - ?body : It works!INFO - express-factory:139 - 5ms - server is closed
sample2.js
Custom server :var expressFactory = request = expressInstance; expressInstance = ; expressInstancestart { request;};
Result :
$ node samples/sample2INFO - express-factory:125 - 180ms - server is listening on localhost:3001HTTP - logger:64 - 20ms - 127.0.0.1 - GET /hello - 200 - ?body : Hello World!INFO - express-factory:139 - 5ms - server is closed
sample3.js
Unix socket :var expressFactory = request = path = mkdirp = util = tmpDir = path socketPath = path expressInstance; expressInstance = ; mkdirp; expressInstancestart { request;};
Result :
$ node samples/sample3INFO - express-factory:122 - 183ms - server is listening on socket /home/openhoat/dev/nodejs/express-factory/tmp/test.sockHTTP - logger:64 - 19ms - undefined - GET / - 200 - ?body : It works!INFO - express-factory:140 - 5ms - server is closed
sample4.js
HTTPS :var expressFactory = request = path = fs = assetsDir = path expressInstance; expressInstance = ; expressInstancestart { request;};
Result :
$ node samples/sample3INFO - express-factory:122 - 183ms - server is listening on socket /home/openhoat/dev/nodejs/express-factory/tmp/test.sockHTTP - logger:64 - 19ms - undefined - GET / - 200 - ?body : It works!INFO - express-factory:140 - 5ms - server is closed
sample5.js
Promises :var expressFactory = Promise = get = Promise expressInstance; expressInstance = ; expressInstance start ;
Result :
$ node samples/sample5INFO - express-factory:126 - 188ms - server is listening on localhost:3000HTTP - logger:64 - 20ms - 127.0.0.1 - GET / - 200 - ?body : It works!INFO - express-factory:140 - 6ms - server is closed
sample6.js
EJS :var expressFactory = path = ejs = expressInstance; expressInstance = ;expressInstancestart;
Result :
Enjoy !