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 🙏

© 2025 – Pkg Stats / Ryan Hefner

w-srvr

v2.2.1

Published

A simple web server configurator for Expressjs

Downloads

10

Readme

W-SRVR

NPM Version

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.

Table of contents

Installation

This module is available on npm, so you could just:

$ npm install w-srvr

Configurators

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));

Other examples

Website

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:


Web service

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:


HTTPS

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:


CORS

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).


Body parsing

The advanced configurator can also enable request body parsing:

server.api
   .post(...)
      .advanced
      .parseJSON({limit: '400kb'})
      .parseURLEncoded()
      .done()
   .get(...)

See:


Testing

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

Feedback

If you want a feature to be added or give some other feedback, feel free to open an issue.

License

MIT