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-object-router

v1.3.5

Published

This library facilitate describing endpoints for REST API servers. The main feature of this library is determining method, path, controller, middlewares, and validation of query object, url object and body object with one object.

Downloads

12

Readme

express-object-router

This library facilitate describing endpoints for REST API servers. The main feature of this library is determining method, path, controller, middlewares, and validation of query object, url object and body object with one object.

If you have any ideas or question just leave issue here

Getting Started

Follow to install module

  • npm install express-object-router --save
  • Look example.

Example

More examples here

Library configuration

/* app.js */
const express = require('express');
const { createRouter } = require('express-object-router');
const path = require('path');

const customMiddleware = (req, res, next) => {
  req.user = {
    id: 10,
    name: 'John',
    age: 20,
  };

  next();
};

const app = express();

const router = createRouter({
  routesPaths: ['./routes/*.js'], /* routes paths  */
  pathsRelateTo: __dirname, /* set current work directory */
  routePrefix: 'v1', /* prefix before every route (i.e. /v1/route/path) */
  extraControllerProps: ['user'], /* properties which will be added to controller from req object */
  middlewaresSequence: ({ /* determining middlewares sequence for every route */
    PARAMS_VALIDATION,
    QUERY_VALIDATION,
    BODY_VALIDATION,
    ROUTER_MIDDLEWARES,
  }) => [
    PARAMS_VALIDATION,
    QUERY_VALIDATION,
    BODY_VALIDATION,
    customMiddleware,
    ROUTER_MIDDLEWARES,
  ],
  defaultValidation: { /* default routes validation */
    query: {
      accessToken: joi.string(),
    },
    body: {},
    params: {},
  },
  onReply: data => ({ data }), /* reply object builder */
});

app.use(router);
app.listen(8080);

Describing routes

/* routes/main.js */
const { GET, POST } = require('express-object-router/methods');
const joi = require('joi');

const testCtrl = ({
  req, /* express req object */
  res, /* express res object */
  next, /* express next function */
  reply, /* function for sending reply to client */
  error, /* function calls error */
  headers, /* headers object from req */
  params, /* req.params */
  body, /* req.body */
  query, /* req.query */
  user, /* property which determined in "extraControllerProps" */
}) => {
  reply({
    user,
    ok: 1,
  });
};

const testMiddleware = ({
  req, /* express req object */
  res, /* express res object */
  next, /* express next function */
  headers, /* req.headers */
  error, /* function calls error */
  pass, /* function pass to next middleware */
  props, /* values from "middlewaresProps" */
  setToReq, /* function assign property to req object */
  params, /* req.params */
  body, /* req.body */
  query, /* req.query */
}) => {
  pass();
};
module.exports = [
  {
    method: GET, /* http method */
    path: '/article', /* route path */
    controller: testCtrl, /* controller function */
    middlewares: [testMiddleware], /* middlewares for this route */
    middlewaresProps: {}, /* props for middleware */
    validation: { /* validation */
      query: {
        limit: joi.number(),
        offset: joi.number(),
      },
      body: {},
      params: {},
    },
  },
];

Changelog

[1.3.4] - 2018-05-25

Updated

  • errorP now avaliable from package.
  • reply now supports promises as argument

[1.3.3] - 2018-04-18

Added

  • errorP fuction to controller params

[1.3.1] - 2018-04-09

Updated

  • interface of build-docs has been changed
  • fix apidoc text generation

[1.3.0] - 2018-04-06

Added

  • Add info to the Route interface
  • Add cli command oe-router build-docs (alpha)
  • Remove spread operator for node 6 compatibility

[1.2.0] - 2017-12-20

Added

  • Update package-lock.json
  • Add body, params and query to middleware object of arguments

MIT License

Copyright (c) 2017 Artur A.