UNPKG

7.3 kBMarkdownView Raw
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
5Novation-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
7Get 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
140.1.6
15
16## How to use
17
18In 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
52Each option should be customized for your app.
53
54#### Config Options:
55
561. **appName**: Name of your app.
571. **server**: Name of the server that the current code is running on.
581. **port:** What port to run server on. Defaults to process.env.PORT and then to 4050.
591. **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.
601. **favicon:** Location of your favicon. Defaults to 'public'.
611. **[envLocation](#environmental-variables)**: Location of your environmental variables.
621. **[preContent](#routes)**: Location of your routes that run before api routes.
631. **[postContent](#routes)**: Location of your routes that run after api routes.
641. **[mongooseSchemaLocation](#mongoose-schema)**: Location of your mongoose schema. Defaults to '_schema.js'.
651. **viewEngine:** Which view engine to use. Example: jade, html, handlebars, etc.
661. **viewEngine:** Which directory to be used to serve views, if using dynamic views.
671. **publicDirectory:** Which directory to be used as your 'static folder.'
681. **servers**: An array of servers that is used by redis-logger and socket.io-online-users.
691. **logger.username**: username to access the redis-logger
701. **logger.password**: password to access the redis-logger
711. **[api.location](#standard-apis)**: Location of your api folder.
721. **[api.safeMode](#standard-apis)**: Safemode prevents exporting variables other than functions using API.
731. **[api.version](#standard-apis)**: The version number the server should use for internal calls.
741. **onlineUsersConfig:** An object with configuration options to use socket.io-online-users.
751. **onlineUsersConfig.roomsToListenTo**: Socket.io rooms to listen to.
761. **onlineUsersConfig.expireUser**: When a user loses connection, how long to keep the user in memory until deleting them.
771. **ssl**: An object of options to use ssl on your node server.
781. **ssl.key:** Location of key file to use.
791. **ssl.cert:** Location of the cert file to use.
801. **ssl.port:** Port to have your node.js https server run on.
811. **sslRedirect:** Redirect http to https.
821. **dontUseRedisTTL:** do not use a ttl for redis.
831. **ttl:** Time in seconds until redis expires documents. Defaults to 3600.
841. **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
92Allows 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
110Allows 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
123Allows you to create APIs that can be accessed by both socket.io and by RESTful requests.
124
125Say 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
137The contents of ``api/SomeAPI.js`` then look like:
138 exports.run = function() {
139 console.log(data.testData); // prints "I Am Groot"
140
141 var number = Math.random();
142 if (number < .5) {
143 return fn('This is a standard error message.');
144 } else {
145 return fn(null, {
146 data: 'This the standard way to send data back to the client.'
147 });
148 }
149 };
150**Note:** extras has the following data:
1511. mongoose, access to the mongoose variable.
1522. io
1533. socket, the particular socket connection, if available
1544. connectionType, either socket, http, or internal.
1555. fileName, the file that the API is being hit by.
1566. req, if available
1577. res, if available
1588. method, the method that is being called.
159
160### Mongoose Schema
161
162##### Location: schema.js
163
164Allows you to create a mongoose schema that can be used throughout your app. Configure your file to look like this:
165
166 var mongoose = require('mongoose');
167 var Schema = mongoose.Schema;
168
169 exports.User = mongoose.model('User', new Schema({
170 createdAt: Date,
171 updatedAt: {
172 type: Date,
173 default: Date.now
174 },
175 firstName:String,
176 lastName:String,
177 fullName:String
178 }));
179**Note:** everything you export in here will be attached to the global scope. It will be accessible throughout your whole server.
180
181
182[license-image]: http://img.shields.io/badge/license-MIT-blue.svg?style=flat-square
183[license-url]: https://github.com/chapinkapa/novation-mobile/blob/master/LICENSE
184
185[npm-version-image]: http://img.shields.io/npm/v/novation-mobile.svg?style=flat-square
186[npm-downloads-image]: http://img.shields.io/npm/dm/novation-mobile.svg?style=flat-square
187[npm-url]: https://npmjs.org/package/novation-mobile
188
189[travis-image]: http://img.shields.io/travis/chapinkapa/novation-mobile.svg?style=flat-square
190[travis-url]: http://travis-ci.org/chapinkapa/novation-mobile
\No newline at end of file