1 | [![NPM version][npm-version-image]][npm-url] [![NPM downloads][npm-downloads-image]][npm-url] [![MIT License][license-image]][license-url] [![Build Status][travis-image]][travis-url]
|
2 |
|
3 | # Novation-Mobile
|
4 |
|
5 | Novation-Mobile is a framework using a NodeJS/MongoDB + Mongoose/Socket.IO/Redis stack. It was built by [Novation Mobile](http://www.novationmobile.com) to create scaleable Node.js servers with an emphasis on quick, standardized development.
|
6 |
|
7 | Get the source from [GitHub](https://github.com/chapinkapa/novation-mobile) or install via NPM
|
8 |
|
9 | npm install novation-mobile --save
|
10 | **Note:** this will take a while. We include all the dependencies to run this.
|
11 |
|
12 | ## Version
|
13 |
|
14 | 0.1.6
|
15 |
|
16 | ## How to use
|
17 |
|
18 | In a web.js file at your project root, use the following to set up a novation-mobile server:
|
19 |
|
20 | var nm = require('novation-mobile');
|
21 |
|
22 | var config = {
|
23 | appName: 'ExampleApp',
|
24 | server: 'Main',
|
25 | port: process.argv[2] || 4050,
|
26 | useStaticServer: true,
|
27 | favicon: 'favicon.ico',
|
28 | envLocation: '_env.js',
|
29 | preContent: 'routes.js',
|
30 | postContent: 'routes2.js',
|
31 | mongooseSchemaLocation: '_schema.js',
|
32 | viewEngine: 'jade',
|
33 | viewDirectory: 'views',
|
34 | publicDirectory: 'public',
|
35 | servers: ['Main:' + os.hostname()],
|
36 | logger:{
|
37 | userName: "",
|
38 | password: ""
|
39 | },
|
40 | api: {
|
41 | location: "api",
|
42 | safeMode: false,
|
43 | },
|
44 | onlineUsersConfig: {
|
45 | roomsToListenTo: ['onlineUsers'],
|
46 | expireUser: 20
|
47 | }
|
48 | };
|
49 |
|
50 | nm.extra(__dirname).server(config);
|
51 |
|
52 | Each option should be customized for your app.
|
53 |
|
54 | #### Config Options:
|
55 |
|
56 | 1. **appName**: Name of your app.
|
57 | 1. **server**: Name of the server that the current code is running on.
|
58 | 1. **port:** What port to run server on. Defaults to process.env.PORT and then to 4050.
|
59 | 1. **useStaticServer:** Wether to allow the server to act as a static server for a specified folder. Used with viewEngine, viewDirectory, and publicDirectory. Defaults to true.
|
60 | 1. **favicon:** Location of your favicon. Defaults to 'public'.
|
61 | 1. **[envLocation](#environmental-variables)**: Location of your environmental variables.
|
62 | 1. **[preContent](#routes)**: Location of your routes that run before api routes.
|
63 | 1. **[postContent](#routes)**: Location of your routes that run after api routes.
|
64 | 1. **[mongooseSchemaLocation](#mongoose-schema)**: Location of your mongoose schema. Defaults to '_schema.js'.
|
65 | 1. **viewEngine:** Which view engine to use. Example: jade, html, handlebars, etc.
|
66 | 1. **viewEngine:** Which directory to be used to serve views, if using dynamic views.
|
67 | 1. **publicDirectory:** Which directory to be used as your 'static folder.'
|
68 | 1. **servers**: An array of servers that is used by redis-logger and socket.io-online-users.
|
69 | 1. **logger.username**: username to access the redis-logger
|
70 | 1. **logger.password**: password to access the redis-logger
|
71 | 1. **[api.location](#standard-apis)**: Location of your api folder.
|
72 | 1. **[api.safeMode](#standard-apis)**: Safemode prevents exporting variables other than functions using API.
|
73 | 1. **[api.version](#standard-apis)**: The version number the server should use for internal calls.
|
74 | 1. **onlineUsersConfig:** An object with configuration options to use socket.io-online-users.
|
75 | 1. **onlineUsersConfig.roomsToListenTo**: Socket.io rooms to listen to.
|
76 | 1. **onlineUsersConfig.expireUser**: When a user loses connection, how long to keep the user in memory until deleting them.
|
77 | 1. **ssl**: An object of options to use ssl on your node server.
|
78 | 1. **ssl.key:** Location of key file to use.
|
79 | 1. **ssl.cert:** Location of the cert file to use.
|
80 | 1. **ssl.port:** Port to have your node.js https server run on.
|
81 | 1. **sslRedirect):** Redirect http to https.
|
82 | 1. **dontUseRedisTTL:** do not use a ttl for redis.
|
83 | 1. **ttl:** Time in seconds until redis expires documents. Defaults to 3600.
|
84 | 1. **addSocketsToRoom:** A function that is called every API call that allows you to add a socket/user to a room for socket.io. The function has two arguments: (session, socket);
|
85 |
|
86 | ## Components
|
87 |
|
88 | ### Environmental Variables
|
89 |
|
90 | ##### Location: _env.js
|
91 |
|
92 | Allows you to set environment variables used throughout the app:
|
93 |
|
94 | exports.configureEnvironment = function(app, process) {
|
95 | // required variables
|
96 | process.env['SESSION_KEY'] = 'my_express.sid';
|
97 | process.env['SESSION_SECRET'] = 'exampleSecret';
|
98 | process.env['COOKIE_KEY'] = 'ExampleCookie';
|
99 | process.env.MONGO_URI = '';
|
100 | process.env.REDIS_URI = 'redis://redis:redis@ip:port/dbindex';
|
101 |
|
102 | // add your own
|
103 | process.env['SOME_API_KEY'] = 'aaa111nnn123';
|
104 | };
|
105 |
|
106 | ### Routes
|
107 |
|
108 | ##### Location: routes.js
|
109 |
|
110 | Allows you to create custom routes for your app.
|
111 |
|
112 | exports.content = function(app, io) {
|
113 | // you can use this page for additional, custom routes
|
114 | app.get('/', function(req, res, next) {
|
115 | res.send('This is an example server');
|
116 | });
|
117 | };
|
118 |
|
119 | ### Standard APIs
|
120 |
|
121 | ##### Location: api/
|
122 |
|
123 | Allows you to create APIs that can be accessed by both socket.io and by RESTful requests.
|
124 |
|
125 | Say I want to call the function 'run' under 'SomeAPI'. I can request the API either using ``http://localhost:4050/api/SomeAPI/run`` or by using sockets on the client:
|
126 |
|
127 | socket.emit('api', 'SomeAPI', 'run', {
|
128 | testData: 'I Am Groot'
|
129 | }, function(err, data) {
|
130 | if (err) {
|
131 | console.log(err);
|
132 | } else {
|
133 | console.log(data);
|
134 | }
|
135 | });
|
136 |
|
137 | The contents of ``api/SomeAPI.js`` then look like:
|
138 |
|
139 | module.exports = function(data, fn, session, extras) {
|
140 |
|
141 | exports.run = function() {
|
142 | console.log(data.testData); // prints "I Am Groot"
|
143 |
|
144 | var number = Math.random();
|
145 | if (number < .5) {
|
146 | return fn('This is a standard error message.');
|
147 | } else {
|
148 | return fn(null, {
|
149 | data: 'This the standard way to send data back to the client.'
|
150 | });
|
151 | }
|
152 | };
|
153 |
|
154 | return exports;
|
155 | };
|
156 | **Note:** extras has the following data:
|
157 | 1. mongoose, access to the mongoose variable.
|
158 | 2. io
|
159 | 3. socket, the particular socket connection, if available
|
160 | 4. connectionType, either socket, http, or internal.
|
161 | 5. fileName, the file that the API is being hit by.
|
162 | 6. req, if available
|
163 | 7. res, if available
|
164 | 8. method, the method that is being called.
|
165 |
|
166 | ### Mongoose Schema
|
167 |
|
168 | ##### Location: schema.js
|
169 |
|
170 | Allows you to create a mongoose schema that can be used throughout your app. Configure your file to look like this:
|
171 |
|
172 | var mongoose = require('mongoose');
|
173 | var Schema = mongoose.Schema;
|
174 |
|
175 | exports.User = mongoose.model('User', new Schema({
|
176 | createdAt: Date,
|
177 | updatedAt: {
|
178 | type: Date,
|
179 | default: Date.now
|
180 | },
|
181 | firstName:String,
|
182 | lastName:String,
|
183 | fullName:String
|
184 | }));
|
185 | **Note:** everything you export in here will be attached to the global scope. It will be accessible throughout your whole server.
|
186 |
|
187 |
|
188 | [license-image]: http://img.shields.io/badge/license-MIT-blue.svg?style=flat-square
|
189 | [license-url]: https://github.com/chapinkapa/novation-mobile/blob/master/LICENSE
|
190 |
|
191 | [npm-version-image]: http://img.shields.io/npm/v/novation-mobile.svg?style=flat-square
|
192 | [npm-downloads-image]: http://img.shields.io/npm/dm/novation-mobile.svg?style=flat-square
|
193 | [npm-url]: https://npmjs.org/package/novation-mobile
|
194 |
|
195 | [travis-image]: http://img.shields.io/travis/chapinkapa/novation-mobile.svg?style=flat-square
|
196 | [travis-url]: http://travis-ci.org/chapinkapa/novation-mobile |
\ | No newline at end of file |