Skip to content

A little bit nicer way to configure express.js

License

Notifications You must be signed in to change notification settings

XC-/expressopot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

expressopot

A little bit nicer way to configure express.js

Usage:

var express = require('express');
var expressopot = require('expressopot');

var config = require('./config');

var app = expressopot(express(), config);

// Some other stuff

module.exports = app;

Why is this needed?

Tell you the truth, this is not strictly needed. This project came from personal problems with the way how Express is "traditionally" configured. After using frameworks such as Django, the Express configuration seemed rather ugly and hard to read. While it is more "Javascript-like" way to do the configuration (meaning configuration by using code), I found Django's configuration dictionary easier to read.

What I am trying to say is that this is just an alternate way to configure Express, not that it is some sort of silver bullet :).

Currently supported properties:

  • views

    • Type: Object
    • Valid keys/values:
      • path: String. Path to view folder.
      • engine: String. View engine.
  • middleware

    • Type: Object
    • Valid keys:
      • pre: Array. List of the middleware to be run (functions).
      • post: Array. List of the middleware to be run (functions).
      • http_errors:
        • Type: Object
        • Valid keys/values:
          • key: Number. HTTP status code.
          • value: Handler function or an object containing the handlers. If the value is an object, it must contain at least one of the following pairs:
            • "production": Function
            • "development": Function. If only one is specified in the object, it will be used for both cases.
      • parameters:
  • routes

    • Type: Object
    • Valid keys/values:
      • key: String. Path.
      • value: Router. Router that handles the requests to the specified path.
  • locals

Example config file:

const express = require('express');
const path = require('path');
const logger = require('morgan');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');

var cors = require('./middleware/cors').handler;
var e404 = require('./middleware/error/404').handler;
var e500 = require('./middleware/error/500');

var recipes = require('./routes/recipes');
var tags = require('./routes/tags');
var ingredients = require('./routes/inredients');
var units = require('./routes/units');

const config = {
  views: {
    path: path.join(__dirname, 'views'),
    engine: 'jade'
  },
  middleware: {
    pre: [
      cors,
      logger('dev'),
      bodyParser.json(),
      bodyParser.urlencoded({ extended: false }),
      cookieParser(),
      express.static(path.join(__dirname, 'public'))
    ],
    http_errors: {
      404: e404,
      500: e500
    }
  },
  routes: {
    '/api/recipes': recipes,
    '/api/tags': tags,
    '/api/ingredients': ingredients,
    '/api/units': units
  },
  locals: {
    foo: 'bar'
  }
};

module.exports = config;

About

A little bit nicer way to configure express.js

Resources

License

Stars

Watchers

Forks

Packages