HTTP Agent
An easy to use server manager for express.js, koa and more...
How does it work
This library is a small wrapper that abstracts normal server functionality like: starting, stopping, restarting and checking status. Built on a familiar syntax from the days of /etc/init.d/nginx start|stop|restart
The HttpAgent object creates an internal pointer to the server after it's started and manages the rest of its lifecycle with easy to use helper methods. Any web framework that uses the .listen
method and returns an instance of Server
can be used with this service.
Getting Started
npm install --save httpagent
A basic setup might look like the following:
my-app/
|
|---/app.js
|---/package.json
|---/server.js
|---/node_modules/
|----------------/express/
|----------------/httpagent/
We'll start with our application code, configured in app.js...
app.js
const express = require('express');
const HttpAgent = require('httpagent');
const app = express()
app.get('/', function (req, res) {
res.send('Hello World');
});
module.exports = new HttpAgent(app);
Now we'll see how it's implemented in server.js...
server.js
const app = require('./app');
let server = app.start();
/**
*
* You can also do this!
*
*/
//app.start();
//let server = app.getServer();
For the traditional server use case this above example will most likely be all you need. Lets see a couple other scenarios you might run into where this package might help.
- Unit Testing - We've included an easy to use stop() method to make unit testing easy
- Clear Connections - We've included a restart() method which can allow you to clear connections without having to restart the node.js process
- Checking Server Status - Any code running the agent can query the status of the server for health checks or other purposes
The Methods
Method | Args | Return |
---|---|---|
start | port, cb |
Server Instance |
stop | cb |
Server Instance |
restart | cb |
Server Instance |
status | none | status: string(OFFLINE,STOPPED,RUNNING) |