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

nodeblues

v1.4.0

Published

An easy to use node server meant for rapid prototyping

Downloads

4

Readme

NodeBlues

NodeBlues is a node server targeted at rapid prototyping. It has two main focuses

  1. Make handling request data and sending responses quick and easy
  2. Make persisting data quick and easy

By registering a callback to a route in the Router you will have access to a requestData object that contains the body, query parameters, and the url parameters that were used for the request. You can then send a response by using the "respondWith" function to send a response after you process the request data.

If you want to persist data you can use a Database instance. You can load data every time your server starts to keep testing data, or you can ignore that step and have a fresh testing environment every time the server starts. By calling the save function before a route calls "respondWith" you can save the current state of the database.

Link to JSDocs

Installation

$ npm install nodeblues --save-dev

Run Example Code or Tests

$ npm install # Must be run before running tests
$ npm test    # Runs all tests
$ npm start   # Runs example server

Hello World

const {Router, Server} = require('nodeblues');

let router = new Router();
let server = new Server(router);

router.get('/', (requestData, respondWith) => {
    respondWith(200, 'Hello World!');
});

server.start('localhost', 1337);

Example Code

const {Database, Router, Server} = require('nodeblues');

// Create a database that we can access from our route callbacks
let database = new Database();

// NOTE: If you want data to persist after restarting the server
database.load();


// Define some routes
let router = new Router();

router.get('/test', (requestData, respondWith) => {
    let data = database.get('test');

    respondWith(200, {data});
});

// Access request body data and save db changes
router.post('/test', (requestData, respondWith) => {
    let value = requestData.body.test;

    database.set('test', value);

    // NOTE: Only needed if you want to persist data after restarting the server
    database.save();

    // NOTE: Add a cookie if desired
    requestData.cookieJar.set('testCookie', 'testValue');

    respondWith(200);
});

// Access path params data
router.get('/test/:id', (requestData, respondWith) => {
    let value = requestData.pathParams.id;

    respondWith(200, value);
});

// Example of returning files
router.get('/:filename', (requestData, respondWith) => {
    let pathToFile = requestData.pathParams.filename;
    let mimeType = pathToFile.endsWith('.js') ? 'application/javascript' : 'text/html';

    // NOTE: need to require in 'fs' dependency here
    fs.readFile(pathToFile, 'utf8', (err, html) => {
        if (err) {
            respondWith(400, `Something bad happened [${err}]`, 'text/plain');
        } else {
            respondWith(200, html, mimeType);
        }
    });
});


// Create our server and start it
const host = 'localhost';
const port = 1337;
let server = new Server(router);

server.start(host, port).then(() => {
    console.log(`Running on http://${host}:${port}`); // eslint-disable-line no-console
});

requestData

This object is passed into the route callbacks and contains the body, pathParams, and queryParams from the request.

requestData.body         // Object/String containing the body from the request
requestData.cookieJar    // CookieJar object that can be used to "set" cookies for the response
requestData.pathParams   // Object containing all path parameters
requestData.queryParams  // Object containing the query params

respondWith

This function is passed into the route callbacks and is used to fire off a response. You can pass in a statusCode, responseData, and/or a contentType header to use.

let respondWith = function respondWith (statusCode = 200, responseData = '', contentType = 'text/json') {
    res.statusCode = statusCode;
    res.setHeader('Content-Type', contentType);
    res.setHeader('Cookie', cookieJar.toHeader());
    res.end(JSON.stringify(responseData));
};

For Those Curious

NodeBlues was named after a character from one of my favorite game series Megaman. The character's name in America was Protoman, prototyping, but in Japan he was called Blues.

TODO List

  1. Add an easy tool for testing api routes