namshi/expressjs-utils

Name: expressjs-utils

Owner: Namshi

Description: null

Created: 2017-07-24 12:57:57.0

Updated: 2018-05-13 06:37:57.0

Pushed: 2018-05-13 06:47:27.0

Homepage: null

Size: 12

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

expressjs-utils

This module contains a set of utilities that make our life easier while writing express apps.

Installation
rn add expressjs-utils
m install expressjs-utils
Usage
nst utils = require('expressjs-utils');
start

start(app, port, env)

starts the express server unless you're in the test env

 starts your app on port 8082 with environment set to 'test'
ils.start(app, 8082, 'test');
static

static(app, path)

mounts the static, /public folder

 assuminc your public folder is located at '/../../public'
ils.static(app);

 if it is located somewhere else, just pass the path, relative to the current file.
ils.static(app, '/../public');
getRouter

getRouter(app, prefix)

returns a router that prefixes all routes at /prefix & /prefix/vX or /vX, where X is a specific version of your api. Use it for API versioning & when you need a common prefix. If no API version is passed, that is vX is not present in the url, it will be set to 0 by default. You can access the API Version using req.apiVersion.

t router = utils.getRouter(app, 'api');
uter.get('/cars', (req, res, next) => {
return res.json({...});
;

 Possible endpoint formats
pi/v1/cars //req.apiVersion will be 1 here
pi/cars //req.apiVersion will be 0 here

t router = utils.getRouter(app, '');
uter.get('/people', (req, res, next) => {
return res.json({...});
;

 Possible endpoint formats
1/people //req.apiVersion will be 1 here
eople   
req.apiVersion will be 0 here
errorHandler

errorHandler(app) provides a generic error handler that can be used at the “end” of your app

 Add this after all your routes
ils.errorHandler(app);

 Then in any route you can simply call next(err) whenever an error occurs
uter.post('/cars', async(req, res, next) => {
try {
  let result = await getCars();
} catch (err) {
  next(err);
}
;
httpError

httpError(code=500, message='Internal Server Error')

Throws an error that has an HTTP status code. These errors are public-friendly, therefore their message can be displayed on the API.

The message parameter can either be a string or an object. For example,

t err = utils.httpError(404, {userMessage: 'This product was not found. Please try other products'});

Then, on the client you will be able to do err.userMessage providing that you use our errorHandler(). Otherwise, you'll need to access your custom object via the data attribute of the error object: err.data.userMessage

 if you are using the error handler above, you can do something like this in
 any of your API endpoint
uter.get('/users/:id', async(req, res, next) => {
try {
  let user = await getUser(req.params.id);

  if (!user) {
    return next(utils.httpError(404, 'Not found'));
  }
} catch (err) {
  next(err);
}
;
serveCSV

serveCSV(res, filename, rows)

returns a downloadable csv file built from “rows” which is an array of objects.

uter.get('/data', async(req, res, next) => {
let data = [{name: "Test 0", age: 3}, {name: "Test 1", age: 4}];

return utils.serveCSV(res, "data.csv", data);
;
hc

hc(app)

installs a health check route (/public/hc)

ils.hc(app);

This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.