web-endpoint
Utils for express server
Last updated 3 years ago by yhjor .
UNLICENSED · Original npm · Tarball · package.json
$ cnpm install web-endpoint 
SYNC missed versions from official npm registry.

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

Current Tags

  • 1.6.0                                ...           latest (3 years ago)

16 Versions

  • 1.6.0                                ...           3 years ago
  • 1.5.0                                ...           4 years ago
  • 1.4.0                                ...           4 years ago
  • 1.3.0                                ...           4 years ago
  • 1.2.2                                ...           4 years ago
  • 1.2.1                                ...           4 years ago
  • 1.2.0                                ...           4 years ago
  • 1.1.0                                ...           4 years ago
  • 1.0.7                                ...           4 years ago
  • 1.0.6                                ...           4 years ago
  • 1.0.5                                ...           4 years ago
  • 1.0.4                                ...           4 years ago
  • 1.0.3                                ...           4 years ago
  • 1.0.2                                ...           4 years ago
  • 1.0.1                                ...           4 years ago
  • 1.0.0                                ...           4 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 17
Dependencies (14)
Dev Dependencies (17)

Copyright 2014 - 2016 © taobao.org |