Novel Planetary Movement
Introducing npm Teams! Private packages + team management tools.Learn more »

web-endpoint

1.6.0 • Public • Published

Reusable middlewares, utils, CURD modules for Express.js web server

NPM Version Travis Status

Key features

  • Quickly inject Express.js middleware
  • Reusable CURD modules

Usages

import express from 'express';
import { hmr } from 'web-endpoint';
 
const server = express();
 
hmr(server, webpackDevConfig);

CURD Modules

Encapsulate most reusable CURD actions into separated modules

Actions

One of the HTTP method handlers:

export default Model => ({
  list: (req, res) => {
    Model.find().sort('-creation_date').exec((err, result) => {
      if (err) {
        res.status(500).send(err);
        return;
      }
 
      res.json({ result });
    });
  },
});

It is simple to reuse any CURD actions:

export default actionCreator(model);

Routers

Common interface of CURD routers:

export default (routeName, model, action) => router => {
  router
    .get(`/${routeName}`, action.list)
    .get(`/${routeName}/:id`, action.get)
    .post(`/${routeName}`, action.new)
    .put(`/${routeName}/:id`, action.update)
    .delete(`/${routeName}/:id`, action.delete);
 
  return router;
};

Routes can be extended too.

export default routeCreator(ROUTE_NAME, model, action);

Models

However, the flexibility of mongoose model remains the same:

export const NAME = 'User';
export const ROUTE_NAME = 'users';
 
export const SCHEMA = {
  email: { type: 'String', required: true },
  display_name: { type: 'String', required: true },
  first_name: { type: 'String', required: false },
  last_name: { type: 'String', required: false },
  password_hash: { type: 'String', required: false },
  encoded_password: { type: 'String', required: false },
  phone: { type: 'String', required: false },
  creation_date: { type: 'Date', default: Date.now, required: false },
};
 
const schema = new mongoose.Schema(SCHEMA);
 
export default mongoose.model(NAME, schema);
 

Available middlewares

  • middleware: generic middleware, e.g. body parser and method override
  • errorHandling: next handler for error JSON object
  • hmr: webpack hot module reload
  • listener: Express.js server listener

Keywords

none

Install

npm i web-endpoint

DownloadsWeekly Downloads

17

Version

1.6.0

License

UNLICENSED

Last publish

Collaborators

  • avatar