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

just-rest

v0.0.20-alpha

Published

Simple REST server.

Readme

Just REST

«Just REST» is the NPM package that will help you make simple REST server.

Table of contents

Install

npm i just-rest --save

Use

  1. Make module

    module.exports = {
       
        GET: {
           '/process-info': function(request, response){
               //http://localhost:3002/process-info
               response.resp(process.env);
           },
              
           '/process-info/([0-9]{1,})': function(request, response, matched){
               //http://localhost:3002/process-info/1234
               response.resp(matched);
           },
              
           '/process-info/error': function(request, response){
               //http://localhost:3002/process-info/error
               throw new Error('Internal Server Error');
               response.resp({});
           },
              
           '/process-info/error-401': function(request, response){
               //http://localhost:3002/process-info/error-401
               response.error(401);
           }
        },
        
        POST: {
            '/process-info': async function(request, response){
                let body = request.body;
                response.resp(body);
            }
        }, 
    };

    save module as ./modules/process-info/index.js

  2. Make interceptor

    function defineHeaders(request, response) {
        const CorsAllowHeaders = {
            'Access-Control-Allow-Origin': '*',
            'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
            'Access-Control-Allow-Headers': 'Content-Type, Set-Cookies, Access-Token'
        };
       
        Object.keys(CorsAllowHeaders).forEach((item) => {
            response.setHeader(item, CorsAllowHeaders[item]);
        });
    }
       
    module.exports = {
        ANY: { //All supported methods «GET, POST, PUT, DELETE, OPTIONS»
            '(.+?)': function (request, response) {
                if (!response.finished) {
                    defineHeaders(request, response);
                    if (request.method === 'OPTIONS') {
                        response.statusCode = 200;
                        response.end('');
                    }
                }
                return
            }
        }
    };
    

    save interceptor as ./interceptors/response/corsAllowHeaders.js

  3. Connect modules and interceptors to your app

    const {Modules, Server, Middlewares} = require('just-rest');
       
    Modules.defineResponseInterceptor('./interceptors/response/corsAllowHeaders.js');
    Modules.defineGlobalMiddleware(Middlewares.bodyJson); // body parser
    
    Modules.define('./modules/process-info/index.js');
       
    new Server({Modules, port: 3002});

    Run app

  4. Open url http://localhost:3002/process-info

Props

Props is any variable

Make props

    const {Modules, Server} = require('just-rest');
    
    Modules.define('./modules/use-props/index.js');
   
    let props = {
        test: '123456qwerty',
        date: new Date(),
        func: function(){
            console.log(`cool`)
        }
    };
    
    new Server({Modules, port: 3002, props });

Use props

make file: ./modules/use-props/index.js

module.exports = {

    GET: {
       '/use-props': function(request, response){
           //http://localhost:3002/use-props
           
           this.props.func();
           console.log(this.props.date);
           console.log(this.props.test);
           
           response.resp({});
       }
    }
};

Examples

Middleware

Use middleware in your module

const {Errors} = require('just-rest');

function user(request, response, match){
    let instance = this;

    //TODO get data from real database

    instance.user = {
        username: 'Guest',
        permissions: [],
        isAuthorized: false
    };

    let testDatabase = {
        'token1': {
            username: 'Boris',
            permissions: ['all'],
            isAuthorized: true
        },
        'token2': {
            username: 'User 2',
            permissions: ['read.me'],
            isAuthorized: true
        },
        'token3': {
            username: 'User 3',
            permissions: ['read.something'],
            isAuthorized: true
        },
    }

    if (request.headers.token && testDatabase.hasOwnProperty(request.headers.token)) {
        instance.user = testDatabase[request.headers.token];
    }

    return;
}

function isAuthorized() {
    if (!this.user.isAuthorized) {
        throw  new Errors(401)
    }
    return;
}

function readPermission() {
    if (!this.user.permissions.includes('read.me') && !this.user.permissions.includes('all')) {
        throw  new Errors(403)
    }
    return;
}

function controller(request, response, matched) {
    response.resp(this.user);
}

module.exports = {
    GET: {
        //use http://localhost:3002/profile/me
        '/profile/me': [
            user, // Using middleware. add user variable to instance
            isAuthorized, // Using middleware. check authorize
            readPermission, // Using middleware. check read permissions
            controller
        ]
    }
};

Set url path

Static url path

module.exports = {
    GET: {
        '/your-url-path-here': function (request, response) {
            //http://localhost:3002/your-url-path-here
            response.resp({success: 'ok'});
        }
    }
};

"Just Rest" supports RegExp expressions. Dynamics url path using RegExp

module.exports = {
    GET: {
        //([0-9]{1,}) = Any number 
        '/user/([0-9]{1,})': function (request, response, matched) {
            //http://localhost:3002/user/1
            let userId = matched[1];
            response.resp({userId});
        }
    }
};