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-wrapper-simple

v1.5.4

Published

Express wrapper with quick custom request and response handling

Downloads

18

Readme

Express Wrapper Simple

Simple express wrapper to provide standard formatted request and response handling. Sample provided in typescript.

Installation

npm i express-wrapper-simple

Quick Start

  • Import express wrapper as main express library
import { Express } from 'express-wrapper-simple';

...

// use express-wrapper-simple Express app:
// include default middlewares - body-parser (express built-in), helmet, cors, compression, morgan (log)
// include expressWrapper middleware
// include expressPagingWrapper middleware
this.express = Express({
  log: {
    enable: true,
    maxFileSize: '50M',
    interval: '1d'
  },
  cors: {
    methods: ['OPTIONS', 'GET', 'PUT', 'PATCH', 'POST', 'DELETE'],
    exposedHeaders: ['X-Auth-Token']
  },
  bodyParser: {
    // optionsJson: {
    //   strict: true
    // },
    optionsUrlencoded: {
      extended: false
    },
  },
});

...

// use imported express normally
this.express.use(otherMiddleWare());
  • Use as express wrapper as middleware
import * as express from 'express';
import { expressWrapper, expressPagingWrapper } from 'express-wrapper-simple';

...

// use default Express app
public express: express.Application = express();

// add expressWrapper middleware - only include express request(req) and response(res) wrapping
this.express.use(expressWrapper);

// add expressPagingWrapper middleware - only include request paging wrapping
this.express.use(expressPagingWrapper);

// use express normally
this.express.use(otherMiddleWare());

Features

  • express application auto include middlewares:
  • expressWrapper
    • extends express request, response, nextFunction to req, resp, next
    • resp implements sendSuccess and sendError function
    • express response wrap with default success or error object
  • expressPagingWrapper
    • auto convert query.* into query.paging.* for easier pagination handling

Examples (expressWrapper)

  • Using ExpressRouter, Req, Resp, response
import { ExpressRouter, Req, Resp, response } from 'express-wrapper-simple';

router: ExpressRouter = ExpressRouter();

...

//
// use built-in 'res.sendSuccess' & 'res.sendError' function
//
this.router.use('/login', (req: Req, res: Resp) => {
  if (!req.body.username) return res.sendError.badRequest('missing body.username');
  if (!req.body.password) return res.sendError.badRequest('missing body.password');

  //
  // login handling logic
  //

  if (unauthorized) return res.sendError.unauthorized('invalid username / password', errorDetail);
  return res.sendSuccess('login successfully', userDetails);
});

//
// different way of 'response.success' & 'response.error' usage
//
this.router.use('/another-login',(req: Req, res: Resp) => {
  if (!req.body.token) return res.status(403).json(response.error('Unauthorized', errorDetails, 'login-error-code'));

  //
  // login handling logic
  //

  return res.status(200).json(response.success('Welcome', userData));
});

...

this.expressApp.use('/api/auth', this.router);

References

  • resp implements:

    • sendSuccess (200)
    • sendError
      • badrequest (400)
      • unauthorized (401)
      • forbidden (403)
      • notfound (404)
      • toomanyrequest (429)
      • unknown (500)
      • maintenance (500)
  • express response wrap with default success and error object

    • success object
      • success (boolean) = true
      • message (string)
      • data (any)
      • paging (any)
    • error object
      • success (boolean) = false
      • message (string)
      • error (any)
      • code (string)
      • maintenance (boolean)
  • req handles:

    • query
      • limit (int) - max limit to 500
      • sortDir (string) - value: ['asc', 'desc']
      • sortBy (string)
      • pagingMode (string)
      • page (int)
      • nextToken (string)
    • [NEW] query.paging (convert query above into query.paging)
      • limit (int) - default: 15
      • sortDir (string) - default: asc
      • sortBy (string) - default: _id
      • pagingMode (string) - default: nopaging
      • page (int) - default: 1
      • sort (list of [sortBy]: [sortDir], key/value pairs)
      • nextToken (string)