Releases: bytesleo/express-easy-helper
Releases · bytesleo/express-easy-helper
2.5.1
2.0.0
- Replaced method "result" to "success"
- ES6
- Webpack
express-easy-helper
Simple helper module for express
Installation
npm
npm install express-easy-helper --save
yarn
yarn add express-easy-helper
Example
import { success, forbidden } from "express-easy-helper";
import express from "express";
const app = express();
app.get("/", (req, res) => {
success(res, "hello");
});
app.get("/about", (req, res) => {
success(res, {"hello" : "world"});
});
app.get("/protected", (req, res) => {
forbidden(res);
});
Method's
// 200
success(res);
// 400
badRequest(res);
// 401
unauthorized(res);
// 403
forbidden(res);
// 404
notFound(res);
//405
unsupportedAction(res);
//422
invalid(res);
// 500
error(res);
Options
// Basic
success(res);
// Code status
success(res, 201);
// Object
success(res, { result: "ok" });
// Code status and Object
success(res, 201, { object });
License
MIT © Leonardo Rico
1.1.0
express-easy-helper
Simple helper module for Express, return promises and simple response's
Installation
NPM
npm install express-easy-helper --save
Git
git clone https://github.com/kevoj/express-easy-helper
cd express-easy-helper
npm i
Examples
ES6 (promises)
import express from 'express';
import {result, notFound, error} from 'express-easy-helper';
const app = express();
import Hello from 'models/hello.model';
app.get('/', (req, res) => {
Hello.find().exec()
.then(notFound(res)) // Return status 404
.then(result(res)) // Return status 200 and Hello's object
.catch(error(res));// Return status 500 with error
});
ES5
var express = require('express');
var api = require('express-easy-helper');
var app = express();
// Return status 200 with text 'Hello world'
app.get('/', function(req, res) {
return api.result(res, 'Hello world');
});
// Return status 201 with text 'Hello world'
app.get('/', function(req, res) {
return api.result(res, 201 ,'Hello world!');
});
// Return status 200 with Object
app.get('/', function(req, res) {
return api.result(res, {message: 'Hello world!'});
});
// Return status 201 with Object
app.get('/', function(req, res) {
return api.result(res, 201 ,{message: 'Hello world!'});
});
Method's
// 200
result(res)
// 400
badRequest(res)
// 401
unauthorized(res)
// 403
forbidden(res)
// 404
notFound(res)
//405
unsupportedAction(res)
//422
invalid(res)
// 500
error(res)
License
MIT © Leonardo Rico