1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | require('colors');
|
11 |
|
12 | var _ = require('underscore'),
|
13 | express = require('express'),
|
14 | ejs = require('ejs'),
|
15 | fs = require('fs');
|
16 |
|
17 |
|
18 | module.exports.Backbone = Backbone = require('backbone');
|
19 |
|
20 |
|
21 |
|
22 | Backbone.Server = function (options) {
|
23 |
|
24 |
|
25 | var settings = {
|
26 | 'database' : false,
|
27 | 'express' : express.createServer(),
|
28 | 'port' : 8000,
|
29 | 'public' : 'public',
|
30 | 'routes' : {},
|
31 | 'socketio' : false,
|
32 | 'views' : 'views',
|
33 | 'view engine' : 'ejs'
|
34 | };
|
35 |
|
36 |
|
37 | _.extend(this, settings, options);
|
38 |
|
39 |
|
40 | this._setup();
|
41 | };
|
42 |
|
43 |
|
44 |
|
45 | _.extend(Backbone.Server.prototype, Backbone.Events, {
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 | load: function(module) {
|
54 |
|
55 | var dir = require.main.filename.split("/").slice(0, require.main.filename.split("/").length - 1).join("/"),
|
56 | files = fs.readdirSync( dir + "/" + module, 'utf-8');
|
57 |
|
58 | files.forEach(function(file) {
|
59 | require( dir + "/" + module + "/" + file );
|
60 | });
|
61 |
|
62 | },
|
63 |
|
64 |
|
65 |
|
66 | route: function (method, path, action) {
|
67 | this.routes[path] = [method, action];
|
68 | },
|
69 |
|
70 | get: function(path, action) {
|
71 | this.route('get', path, action);
|
72 | },
|
73 |
|
74 | post: function(path, action) {
|
75 | this.route('post', path, action);
|
76 | },
|
77 |
|
78 | put: function(path, action) {
|
79 | this.route('put', path, action);
|
80 | },
|
81 |
|
82 | delete: function(path, action) {
|
83 | this.route('delete', path, action);
|
84 | },
|
85 |
|
86 | _setup: function() {
|
87 |
|
88 | var server = this;
|
89 |
|
90 |
|
91 | this.express.use(express.static(this.public));
|
92 |
|
93 |
|
94 | this.express.set('view engine', this['view engine']);
|
95 |
|
96 |
|
97 |
|
98 |
|
99 | if ( this.socketio ) {
|
100 |
|
101 | this.io = require('socket.io').listen(this.express);
|
102 |
|
103 |
|
104 | this.bind = function(name, action) {
|
105 | this.io.sockets.on(name, action);
|
106 | };
|
107 |
|
108 | this.send = function(name, values) {
|
109 | this.io.sockets.emit(name, values);
|
110 | };
|
111 |
|
112 | this.events = function(events) {
|
113 |
|
114 | for (var e in events) {
|
115 | this.bind(e, events[e]);
|
116 | }
|
117 |
|
118 | };
|
119 |
|
120 | console.log("Socket.IO engaged.".blue);
|
121 | }
|
122 |
|
123 |
|
124 |
|
125 | if (this.database.adapter === 'mongo') {
|
126 |
|
127 | var Schema = require('./backbone-mongo').Schema;
|
128 |
|
129 | require('./backbone-mongo').setup(this.database);
|
130 |
|
131 | this.Datamodel = {
|
132 | mongoose: require('./backbone-mongo').mongoose
|
133 | };
|
134 |
|
135 | this.Datamodel.register = function(name, schema) {
|
136 |
|
137 | if (!name) {
|
138 | throw new Error("\nPlease provide a name for this MongoDB model.\n".red.bold)
|
139 | }
|
140 | if (!schema) {
|
141 | throw new Error("\nPlease provide a schema for ".red.bold + name.red.bold + ".\n".red.bold);
|
142 | }
|
143 |
|
144 | var new_schema = new Schema(schema);
|
145 |
|
146 | server.Datamodel[name] = mongoose.model(name, new_schema);
|
147 | };
|
148 |
|
149 | }
|
150 | },
|
151 |
|
152 |
|
153 |
|
154 | start: function() {
|
155 |
|
156 |
|
157 | for (var i in this.routes) {
|
158 |
|
159 | var method = this.routes[i][0],
|
160 | action = this.routes[i][1];
|
161 |
|
162 | this.express[method](i, action);
|
163 | }
|
164 |
|
165 |
|
166 | this.express.get('/backbone-server/backbone.js', function(req, res) {
|
167 |
|
168 | var underscorejs = fs.readFileSync(__dirname + '/node_modules/underscore/underscore-min.js', 'utf-8'),
|
169 | backbonejs = fs.readFileSync(__dirname + '/node_modules/backbone/backbone.js', 'utf-8');
|
170 |
|
171 | res.contentType('text/javascript');
|
172 | res.send(underscorejs + "\n" + backbonejs);
|
173 |
|
174 | });
|
175 |
|
176 | this.express.listen(this.port);
|
177 |
|
178 | console.log("Backbone Server is listening on port ".blue + this.port.toString().yellow);
|
179 | }
|
180 |
|
181 | }); |
\ | No newline at end of file |