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-toybox

v0.2.0

Published

My Own Extra Stuff for Express

Downloads

42

Readme

express-toybox

My Own Extra Stuff for Express.

getting started

install module via npm

$ npm install express-toybox

install from git and generate documents

$ git clone http://github.com/iolo/express-toybox.git
$ cd express-toybox
$ npm install
$ grunt docs
$ open build/doxx/index.html

run example

$ export DEBUG='*'
$ node example/app.js
$ open http://localhost:3333

require it with/without express

var express = require('express-toybox')(require('express'));
//var express = require('express-toybox')();
express.toybox.utils.collectQueryParams(req);

or

var express = require('express'),
    expressToybox = require('express-toybox');
expressToybox.utils.collectQueryParams(req);
//express.toybox.utils.collectQueryParams(req);

features

errors

  • StatusCode
  • StatusLine
  • HttpError
  • ClientError
  • BadRequest
  • Unauthorized
  • Forbidden
  • NotFound
  • ServerError
  • InternalServerError
  • NotImplemented
  • ...

commons

server

  • start()
  • stop()

utils

  • collectQueryParams()
  • pagination()
  • renderViewOrRedirectToNext()
  • echo()
  • extendHttpRequest() - additional methods for express.request
    • req.strParam(name, fallback)
    • req.intParam(name, fallback)
    • req.numberParam(name, fallback)
    • req.boolParam(name, fallback)
    • req.dateParam(name, fallback)
    • req.collectParams(names)
    • ...
  • extendHttpResponse() - additional methods for express.response
    • res.sendCallbackFn(next, status)
      var fs = require('fs');
      app.get('/foo', function (req, res, next) {
              fs.readFile('file.txt', res.sendCallbackFn(next));
      });
    • res.jsonCallbackFn(next, status)
    • res.jsonpCallbackFn(next, status)
    • res.sendFileCallbackFn(next, status)
    • res.redirectCallbackFn(next, status)
    • res.renderCallbackFn(view, next, status)
    • res.sendLater(promise, next, status)
      var FS = require('q-io/fs');
      app.get('/bar', function (req, res, next) {
              res.sendLater(FS.readFile('file.txt'), next);
      });
    • res.jsonLater(promise, next, status)
    • res.jsonpLater(promise, next, status)
    • res.sendFileLater(promise, next, status)
    • res.redirectLater(promise, next, status)
    • res.renderLater(view, promise, next, status)
    • ...
  • ...

cors middleware

  • usage
express()...use(express.toybox.cors(config))...
  • config
{
TBW: ...
}

logger middleware

  • usage
express()...use(express.toybox.logger(config))...
  • log to console using morgan: 'combined', 'common', 'short', 'tiny', 'default' or 'dev'
  • log to file using morgan
{
    file:'path-to-log-file',
    format:'morgan-format',
    morgan-options...
}
  • log to debug using morgan-debug
{
    debug:'debug-namespace',
    format:'morgan-format',
    morgan-debug-options...
}
  • ...

session middleware

  • usage
express()...use(express.toybox.session(config))...
  • express-session with memory store: {express-session-options...}
  • express-session with custom store:
{
    store:{
        module:'store-module-name',
        store-module-options...
    },
    express-session-options...
}
  • ...

resource routes

  • usage
express()...useResource(path, resource-module)...
  • example
useResource('/posts/:id', {
    // get /posts
    index: function (req, res) { ... },
    // post /posts
    create: ...
    // get /posts/123
    show: function (req, res) { assert(req.param('id') == 123)... }
    // put /posts/123
    update: ...
    // delete /posts/123
    destroy: ...
    ...
});

error404 error handler

send custom http 404 error with json/html/text by accept header in request.

  • usage
express()...use(express.toybox.error404(config))...
  • config
{
    code: custom-error-code,
    message:'custom-error-message',
    template: 'lodash-micro-template-string-for-404-error-page',
    view:'path-view-template'
    ...
}

error500 error handler

send custom http error with json/html/text by accept header in request.

  • usage
express()...use(express.toybox.error500(config))...
  • config
{
    status: custom-status-code,
    code: custom-error-code,
    mappings:{'err.name':{err-response-body...}, 'err.code': {err-response-body...}, ...},
    stack:true/false,
    template: 'lodash-micro-template-string-for-error-page',
    view:'path-view-template',
    ...
}

declarative middlewares

  • usage
express()...useCommonMiddlewares(config)...

or

var app = express();
...
express.toybox.common.configureMiddlewares(app, config);
...
  • config
{
    logger: {logger-config...},
    compress: {compress-config...},
    cookieParser: {cookieParser-config...},
    methodOverride: {methodOverride-config...},
    cors: {cors-config...},
    session: {session-config...},
    csrf: {csrf-config...},
    multipart: {multipart-config...},
    urlencoded: {urlencoded-config...},
    json: {json-config...},
    text: {text-config...},
    raw: {raw-config...},
    ...
}

logger

compress(or compression)

{
TBW: ...
}

cookieParser

{
TBW: ...
}

methodOverride

{
TBW: ...
}

cors

  • configure cors middleware(custom).

session

csrf(or csurf)

  • configure csurf middleware(contrib).
{
TBW: ...
}

multipart

  • configure multiparty middleware(by andrewrk) for multipart/form-data request.
{
TBW: ...
}

urlencoded

  • configure body-parser urlencoded middleware(contrib) for application/x-www-form-urlencoded request.
{
TBW: ...
}

json

  • configure body-parser json middleware(contrib) for application/json request.
{
TBW: ...
}

text

  • configure body-parser text middleware(contrib) for plain/text request.
{
TBW: ...
}

raw

  • configure body-parser raw middleware(contrib) for application/octet-stream request.
{
TBW: ...
}

declarative routes

  • usage
express()...useCommonRoutes(config)...

or

var app = express();
...
express.toybox.common.configureRoutes(app, config);
//expressToybox.common.configureRoutes(app, config);
...
  • config
{
    root: path-to-directory,
    statics: {statics-config...},
    resources: {resources-config...},
    errors: {errors-config...},
    ...
}

root

statics

{
    'url-path': 'path-to-static-content-directory'
    ...
}

resources

  • configure multiple resource routes(custom).
{
    'url-path': 'resource-module-name'
    ...
}

errors

{
    '404': {error404-config...},
    '500': {error500-config...},
    ...
}

error404

  • configure error404 error handler(custom).

error500

may the source be with you...