spur-web
Common node modules and express middleware that are designed to be the boilerplate of a node web app.
Last updated 3 years ago by acolchado .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install spur-web 
SYNC missed versions from official npm registry.
Spur: Web

Common node modules and express middleware that are designed to be the boilerplate of a node web app.

NPM version Build Status

About the Spur Framework

The Spur Framework is a collection of commonly used Node.JS libraries used to create common application types with shared libraries.

Visit NPMJS.org for a full list of Spur Framework libraries >>

Topics

Quick start

Installing

Dependencies:

$ npm install --save spur-ioc spur-common spur-config

Module:

$ npm install --save spur-web

Note: The example code below expects that you are using Node 6. We follow the active Node LTS schedule.

Usage

src/config/*

For an example of the configuration, please take a look at this example: example/src/config/.

src/injector.js

const path = require('path');
const spur = require('spur-ioc');
const spurCommon = require('spur-common');
const registerConfig = require('spur-common/registerConfig');
const spurWeb = require('spur-web');

module.exports = function () {
  const ioc = spur.create('example');

  // Register configuration
  registerConfig(ioc, path.join(__dirname, './config'));


  ioc.merge(spurCommon());
  ioc.merge(spurWeb());

  // register folders in your project to be auto-injected
  ioc.registerFolders(__dirname, [
    'controllers/',
    'runtime/'
  ]);

  return ioc;
};

src/runtime/WebServer.js

module.exports = function (BaseWebServer, path) {
  class WebServer extends BaseWebServer {

    // Add additional changes to the middleware by overriding the method
    registerDefaultMiddleware() {
      super.registerDefaultMiddleware();
      this.registerEjsTemplates();
    }

    registerEjsTemplates() {
      this.logSectionHeader('EJS Template Registration');

      this.app.set('view engine', 'ejs');
      this.app.set('views', path.join(__dirname, '../views'));
    }
  }

  // Assure there is just one instance
  return new WebServer();
};

src/views/hello.ejs

<html>
  <head>
     <meta charset="utf-8">
  </head>
<body>
  <h1><%= user %></h1>
</body>
</html>

src/controllers/HelloController.js

Files ending in *Controller.js are auto registered as controllers.

module.exports = function (BaseController) {
  class HelloController extends BaseController {

    configure(app) {
      super.configure(app);

      app.get('/', this.getRoot.bind(this));
      app.get('/hello', this.getHello.bind(this));
    }

    getRoot(req, res) {
      res.status(200).send('This is the root page defined in HelloController.js.');
    }

    getHello(req, res) {
      const model = {
        user: req.query.user || 'John Doe'
      };

      res.render('hello', model);
    }

  }

  return new HelloController();
};

start.js

const injector = require('./src/injector');

// IMPORTANT: The callback needs to be a function call vs. using a fat-arrow block. Fat-arrow is not supported yet.
injector().inject(function (UncaughtHandler, WebServer, Logger, config, configLoader, nodeProcess) {
  UncaughtHandler.listen();

  Logger.info(`NODE_ENV: ${nodeProcess.env.NODE_ENV}`);
  Logger.info(`PORT: ${config.Port}`);
  Logger.info(`CONFIG: ${configLoader.configName}`);

  WebServer.start()
  .then(() => {
    // Execute other logic after the server has started
  });
});

Running example

$ npm start

Available dependencies in injector

To see the latest list of the default dependencies that are injected, check out the injector.js file. Here is a short list of of all of the dependencies available:

Libraries

List of external dependencies used and exposed by spur-web. They can be found at npmjs.org using their original names.

Name Original Module Name
express express
expressDevice express-device
methodOverride method-override
cookieParser cookie-parser
bodyParser body-parser
expressWinston express-winston
ejs ejs

Local dependecies

All of the files under the src/ directory are made available when this module is merged into another injector. The following list are the notable dependencies available.

Reusable

Name Source Description
BaseController code A base class in order to be able to identify all of the controllers derived from it.
BaseWebServer code A base web server that sets all of the middleware mentioned here.
ControllerRegistration code Registers all of the controllers based on the BaseController type and also files that end with Controller
BaseMiddleware code A base class in order to be able to identify all of the middleware derived from it.

Used internally, but can be used/replaced

Name Source Description
HtmlErrorRender code Sets basic error rendering for uncaught errors.
DefaultMiddleware code Registers default express middleware: cookie parser, body parser, method override, and express device
ErrorMiddleware code Adds error handling for unhandled errors for requests.
NoCacheMiddleware code Middleware for no cache headers
PromiseMiddleware code Extends the response object with functionality to be used through promises. It unwraps promises as they are being resolved.
WinstonRequestLoggingMiddleware code Winston middleware for logging every request to the console log.

Contributing

We accept pull requests

Please send in pull requests and they will be reviewed in a timely manner. Please review this generic guide to submitting a good pull requests. The only things we ask in addition are the following:

  • Please submit small pull requests
  • Provide a good description of the changes
  • Code changes must include tests
  • Be nice to each other in comments. :innocent:

Style guide

The majority of the settings are controlled using an EditorConfig configuration file. To use it please download a plugin for your editor of choice.

All tests should pass

To run the test suite, first install the dependancies, then run npm test

$ npm install
$ npm test

License

MIT

Current Tags

  • 2.0.0                                ...           latest (3 years ago)
  • 1.0.0-rc.1                                ...           next (4 years ago)

28 Versions

  • 2.0.0                                ...           3 years ago
  • 2.0.0-rc.1                                ...           3 years ago
  • 1.0.1                                ...           4 years ago
  • 1.0.0                                ...           4 years ago
  • 1.0.0-rc.1                                ...           4 years ago
  • 0.4.1                                ...           4 years ago
  • 0.4.0                                ...           4 years ago
  • 0.3.0                                ...           4 years ago
  • 0.2.0                                ...           5 years ago
  • 0.1.7                                ...           5 years ago
  • 0.1.6                                ...           5 years ago
  • 0.1.5                                ...           5 years ago
  • 0.1.4                                ...           5 years ago
  • 0.1.3                                ...           5 years ago
  • 0.1.1                                ...           5 years ago
  • 0.1.0                                ...           5 years ago
  • 0.0.12                                ...           5 years ago
  • 0.0.11                                ...           6 years ago
  • 0.0.10                                ...           6 years ago
  • 0.0.9                                ...           6 years ago
  • 0.0.8                                ...           6 years ago
  • 0.0.7                                ...           6 years ago
  • 0.0.6                                ...           6 years ago
  • 0.0.5                                ...           6 years ago
  • 0.0.4                                ...           6 years ago
  • 0.0.3                                ...           6 years ago
  • 0.0.2                                ...           6 years ago
  • 0.0.1                                ...           6 years ago
Maintainers (2)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (16)
Dev Dependencies (8)
Dependents (2)

Copyright 2014 - 2017 © taobao.org |