A simple web server configurator for Expressjs
It makes the process of setting up a web server less verbose, by wrapping an Expressjs
server inside a fluent interface
configurator, so method calls can be chained for better readability.
- Installation
- Configurators
- Other examples
- Website
- Web service
- HTTPS
- CORS
- Body parsing
- Testing
- Feedback
- License
This module is available on npm, so you could just:
$ npm install w-srvr
The main Configurator
object gives you access to some settings as well as the inner configurators: StaticConfigurator
with server.static
, and APIConfigurator
with server.api
.
Here is an example:
const Configurator = require('w-srvr');
const server = new Configurator();
server
// *Setting the port:
.port(3000)
// *Configuring the API:
.api
.get('/api/users', (req, res, next) => res.end())
.get('/api/users/:id', (req, res, next) => res.end())
.post('/api/users', (req, res, next) => res.end())
.done()
// *Configuring the static resources:
.static
.add('/static/js', '../src/js')
.add('/static/css', '../src/css')
.index('../src/index.html')
.done()
// *Starting the server:
.start()
.then(output => console.log('Server started at ' + output.address.href))
.catch(err => console.error(err));
Lets say your website content is located under the /src
folder:
├── main.js
└── src
├── css
├── js
├── imgs
└── index.html
How we can serve all the /src
folder content under the /static
route, and setup the index page?
A simple way to do it would be:
main.js
server
// *Setting up the server port:
.port(3000)
// *Setting up the static content, and the index page:
.static
.add('/static/css', './src/css')
.add('/static/js', './src/js')
.add('/static/img', './src/img')
.index('./src/index.html')
.done()
// *Starting the server:
.start()
// *Logging the server address:
.then(output => console.log('Server started at ' + output.address.href))
// *Logging errors:
.catch(err => console.error(err));
See:
- StaticConfigurator
- StaticConfigurator.prototype.add(route, resource_path)
- StaticConfigurator.prototype.index(file)
Is easy to build a simple web service with some routes:
server
// *Setting up the server port:
.port(3000)
// *Setting up the server routes:
.api
.get('/ping', function(req, res, next){
res.status(200)
.send('pong')
.end();
})
.done()
// *Starting the server:
.start()
// *Logging the server address:
.then(output => console.log('Server started at ' + output.address.href))
// *Logging errors:
.catch(err => console.error(err));
As your server starts to grow and gets more complex, you may want to organize your routes in separate files, so lets say you have the following project structure:
├── main.js
└── routes
└── users.js
Considering that users.js
exports some Expressjs middleware functions, we could build a CRUD web service this way:
main.js
// *Getting the users middleware routes module:
const users = require('./routes/users.js');
server
// *Setting up the server port:
.port(3000)
// *Setting up the web service routes:
.api
.get('/api/v1/users', users.getAll)
.get('/api/v1/users/:id', users.getOne)
.post('/api/v1/users', users.insert)
.put('/api/v1/users/:id', users.update)
.delete('/api/v1/users/:id', users.remove)
.done()
// *Starting the server:
.start()
// *Logging the server address:
.then(output => console.log('Server started at ' + output.address.href))
// *Logging errors:
.catch(err => console.error(err));
See:
- APIConfigurator
- APIConfigurator.prototype.add(method, route, middleware)
- APIConfigurator.prototype.get(route, middleware)
- APIConfigurator.prototype.post(route, middleware)
- APIConfigurator.prototype.put(route, middleware)
- APIConfigurator.prototype.delete(route, middleware)
- APIConfigurator.prototype.head(route, middleware)
- APIConfigurator.prototype.patch(route, middleware)
- APIConfigurator.prototype.options(route, middleware)
- APIConfigurator.prototype.all(route, middleware)
- APIConfigurator.prototype.most(route, middleware)
It's simple to configure an HTTPS server using a key and certificate:
server
.https({
key: './my-key.key',
cert: './my-cert.crt'
}, true)
// ...
or using a PFX file:
server
.https({
pfx: './my-pfx.pfx',
passphrase: './my-pass.txt'
}, true)
// ...
See:
The advanced configurator
helps you to setup CORS responses correctly:
server.api
.most('/api/v1/*')
.advanced
.allowedOrigins('*')
.allowedMethods('GET', 'POST')
.allowedHeaders('Content-Type')
.done()
.get(...)
.post(...)
// ...
Now, every origin can make GET
and POST
requests (with a JSON body for example, see Body parsing) to routes inside /api/v1/
without being rejected by cross-origin policies.
You can get further information on CORS here (MDN).
The advanced configurator can also enable request body parsing:
server.api
.post(...)
.advanced
.parseJSON({limit: '400kb'})
.parseURLEncoded()
.done()
.get(...)
See:
- body-parser
- AdvancedAPIConfigurator.prototype.parseJSON(options)
- AdvancedAPIConfigurator.prototype.parseText(options)
- AdvancedAPIConfigurator.prototype.parseRaw(options)
- AdvancedAPIConfigurator.prototype.parseURLEncoded(options)
If you would like to run the tests, you can do it with the test
command:
$ npm test
Make sure you have installed the dev-dependencies
, as the tests will need them:
$ npm install --only=dev
If you want a feature to be added or give some other feedback, feel free to open an issue.