UNPKG

1.18 kBJavaScriptView Raw
1
2'use strict';
3
4// Modules
5var express = require('express');
6var socketio = require('socket.io');
7var config = require('otto-config');
8var response = require('otto-response');
9
10// Export App
11module.exports = function (options) {
12
13 if (!options) { options = {}; }
14
15 // New Express App
16 var app = express();
17
18 // TODO: Add configuration options
19 // Add WebSockets
20 var io = socketio();
21 app.socketio = io;
22
23 // Global Settings
24 config.global(app, process.env, options);
25
26 // TODO: Add configuration option
27 // Serve static files before the routes
28 // app.use(express.static(__dirname + '/../public')));
29
30 // Remove Header X-Powered-By
31 app.disable('x-powered-by');
32
33 // Ensure req.locals exists
34 app.use(function (req, res, next) {
35 if (!req.locals) { req.locals = {}; }
36 next();
37 });
38
39 // Attach Routes
40 if (options.routes) {
41 if (!Array.isArray(options.routes)) { return new Error('options.routes needs to be an Array'); }
42 for (var i = 0; i < options.routes.length; i++) {
43 options.routes[i] (app);
44 }
45 }
46
47 // Catch-All Route (Not Found)
48 app.use(response.not_found);
49
50 // Error Handler
51 app.use(response.failure);
52
53 return app;
54
55};