UNPKG

1.01 kBJavaScriptView Raw
1
2'use strict';
3
4// Modules
5var async = require('async');
6var express = require('express');
7var socketio = require('socket.io');
8var config = require('otto-config');
9var response = require('otto-response');
10
11// Export App
12module.exports = function (options) {
13
14 if (!options) { options = {}; }
15
16 // New Express App
17 var app = express();
18
19 // Add WebSockets
20 app.socketio = socketio();
21
22 // Global Settings
23 config.global(app, process.env, options);
24
25 // Serve static files before the routes
26 if (options.static) {
27 app.use(express.static(options.static));
28 }
29
30 // Attach Routes Asynchronously
31 if (options.routes) {
32 if (!Array.isArray(options.routes)) { return new Error('options.routes needs to be an Array'); }
33 async.mapSeries(options.routes, function (route, done) {
34 route(app, done);
35 }, function (error) {
36
37 // Catch-All Route (Not Found)
38 app.use(response.not_found);
39
40 // Error Handler
41 app.use(response.failure);
42
43 });
44 }
45
46 return app;
47
48};