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

nodejs-server-maintenance

v2.1.1

Published

Express.js middleware to control server maintenance mode

Downloads

616

Readme

nodejs-server-maintenance

Express.js middleware to control server maintenance mode

Installation

yarn add nodejs-server-maintenance

or

npm install nodejs-server-maintenance

Usage

Using default options

const maintenance = require('nodejs-server-maintenance');

maintenance(app);

Initializing maintenance mode to true

const maintenance = require('nodejs-server-maintenance');

maintenance(app, true);

Using customized configuration

const maintenance = require('nodejs-server-maintenance');

// these are the default values

const options = {
    mode: false,
    accessKey: "pw1234", // optional
    endpoint: '/maintenance',
    filePath: null,
    useApi: false,
    statusCode: 503,
    message: 'Error 503: Server is temporarily unavailable, please try again lager.', // 503 is taken from statusCode
    blockMethods: ['GET']
}

maintenance(app, options);

Setting a customized access middleware

If you want to handle the access to the endpoint by yourself, you can add a function. It allows you to hide the endpoint API and control the response.

const maintenance = require('nodejs-server-maintenance');

// options

const middleware = (req, res, next) => {
	try {
		// you can verify the token if you use jwt or have other conditions
		const  token  =  req.headers.authorization.split("  ")[1];
		if (token  ===  "token1234") return  next();
		throw  new  Error();
	} catch (error) {
		res.sendStatus(404);	
	}
};

maintenance(app, options, middleware);

Setting maintenance mode on

POST request to http://yourserver/[endpoint]?access_key=[accessKey]

You can also customize the parameters sending a JSON with the fields below you want to change without changing your code (until the app restarts)

{
    filePath: '...',
    useApi: false,
    statusCode: 503,
    message: '...',
    blockMethods: ['GET']
}

Setting maintenance mode off

DELETE request to http://yourserver/[endpoint]?access_key=[accessKey]

In order to reset the customized parameters above, send the following JSON or restart the server

{
    reset: true
}

Getting maintenance status

GET request to http://yourserver/[endpoint]/status

Response:

{
    success: true,
    mode: true/false
}

Notes

  • accessKey is optional but is recommended to use
  • The maintenance should be defined before your app routes and after your body parser
  • The priority of the variables is
    1. useApi - if true the server will send a JSON
    2. filePath - if filePath is provided, the server will send that file
    3. message - if above are false or null, the message will be sent by default

Variables

Name | Type | Description ------------ | ------------- | ------------- mode | Boolean | Sets maintenance mode on/off accessKey | String | Allows access only using the access key endpoint | String | The URL endpoint of the API requests filePath | String | The path to the file to send (the default value is null) message | String | A default message to display useApi | Boolean | If true, the server will send JSON { statusCode, message } statusCode | Number | Response status code blockMethods | Array (strings) | Blocks the request methods during the maintenance