npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

express-factory

v0.0.6

Published

Easy setup an Express instance

Downloads

2

Readme

NPM version Build Status Coverage Status

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 :

require('express-factory')().start();

require('open')('http://localhost:3000/');

Result :

Screen shot

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

Simple express server : sample1.js

var expressFactory = require('express-factory')
  , request = require('request')
  , expressInstance;

expressInstance = expressFactory();

expressInstance.start(function () {
  request.get('http://localhost:3000', function (err, res, body) {
    if (err) {
      console.error(err.stack);
    } else {
      console.log('body :', body); // by default the server returns 'It works!'
    }
    expressInstance.stop();
  });
});

Result :

$ node samples/sample1
INFO  - express-factory:125 - 214ms - server is listening on localhost:3000
HTTP  - logger:64 - 21ms - 127.0.0.1 - GET / - 200 - ?
body : It works!
INFO  - express-factory:139 - 5ms - server is closed

Custom server : sample2.js

var expressFactory = require('express-factory')
  , request = require('request')
  , expressInstance;

expressInstance = expressFactory({
  port: 3001, // custom port (default : 3000)
  routers: { // can be an array
    routes: { // can be an array
      path: '/hello', // uri path of a route
      middleware: function (req, res) { // middleware of the route
        res.type('text').end('Hello World!');
      }
    }
  }
});

expressInstance.start(function () {
  request.get('http://localhost:3001/hello', function (err, res, body) {
    if (err) {
      console.error(err.stack);
    } else {
      console.log('body :', body);
    }
    expressInstance.stop();
  });
});

Result :

$ node samples/sample2
INFO  - express-factory:125 - 180ms - server is listening on localhost:3001
HTTP  - logger:64 - 20ms - 127.0.0.1 - GET /hello - 200 - ?
body : Hello World!
INFO  - express-factory:139 - 5ms - server is closed

Unix socket : sample3.js

var expressFactory = require('express-factory')
  , request = require('request')
  , path = require('path')
  , mkdirp = require('mkdirp')
  , util = require('util')
  , tmpDir = path.join(__dirname, '..', 'tmp')
  , socketPath = path.join(tmpDir, 'test.sock')
  , expressInstance;

expressInstance = expressFactory({handle: socketPath});

mkdirp.sync(tmpDir);

expressInstance.start(function () {
  request.get(util.format('http://unix:%s:/', socketPath), function (err, res, body) {
    if (err) {
      console.error(err.stack);
    } else {
      console.log('body :', body);
    }
    expressInstance.stop();
  });
});

Result :

$ node samples/sample3
INFO  - express-factory:122 - 183ms - server is listening on socket /home/openhoat/dev/nodejs/express-factory/tmp/test.sock
HTTP  - logger:64 - 19ms - undefined - GET / - 200 - ?
body : It works!
INFO  - express-factory:140 - 5ms - server is closed

HTTPS : sample4.js

var expressFactory = require('express-factory')
  , request = require('request')
  , path = require('path')
  , fs = require('fs')
  , assetsDir = path.join(__dirname, '..', 'spec', 'assets')
  , expressInstance;

expressInstance = expressFactory({
  port: 3443,
  ssl: {
    key: fs.readFileSync(path.join(assetsDir, 'key.pem')),
    cert: fs.readFileSync(path.join(assetsDir, 'cert.pem'))
  }
});

expressInstance.start(function () {
  request.get({
    url: 'https://localhost:3443/',
    strictSSL: false
  }, function (err, res, body) {
    if (err) {
      console.error(err.stack);
    } else {
      console.log('body :', body);
    }
    expressInstance.stop();
  });
});

Result :

$ node samples/sample3
INFO  - express-factory:122 - 183ms - server is listening on socket /home/openhoat/dev/nodejs/express-factory/tmp/test.sock
HTTP  - logger:64 - 19ms - undefined - GET / - 200 - ?
body : It works!
INFO  - express-factory:140 - 5ms - server is closed

Promises : sample5.js

var expressFactory = require('express-factory')
  , Promise = require('bluebird')
  , get = Promise.promisify(require('request').get)
  , expressInstance;

expressInstance = expressFactory();

expressInstance
  .start()
  .then(function () {
    return get('http://localhost:3000');
  })
  .spread(function (res, body) {
    console.log('body :', body);
  })
  .then(function () {
    return expressInstance.stop();
  })
  .catch(function (err) {
    console.error(err.stack);
  });

Result :

$ node samples/sample5
INFO  - express-factory:126 - 188ms - server is listening on localhost:3000
HTTP  - logger:64 - 20ms - 127.0.0.1 - GET / - 200 - ?
body : It works!
INFO  - express-factory:140 - 6ms - server is closed

EJS : sample6.js

var expressFactory = require('express-factory')
  , path = require('path')
  , ejs = require('ejs')
  , expressInstance;

expressInstance = expressFactory({
  set: { // express app set
    'view engine': 'html',
    'views': path.join(__dirname, '..', 'templates')
  },
  engine: { // express app engine
    'html': ejs.renderFile
  },
  routers: {
    routes: {
      path: '/',
      middleware: function (req, res) {
        res.render('home', {content: 'Hello!'});
      }
    }
  }
});
expressInstance.start();

Result :

Screen shot hello

Enjoy !