UNPKG

2.18 kBJavaScriptView Raw
1// Backbone Server
2// Nate Hunzaker
3//
4// Adds a module for server side interaction within Backbone using Connect
5
6
7//-- Requirements ------------------------------------------------------//
8
9var Backbone = require('backbone');
10 express = require('express');
11
12
13//-- Backbone Server ---------------------------------------------------//
14
15Backbone.Server = Backbone.Router.extend({
16
17 //-- Default Values ------------------------------------------------//
18
19 'port' : 8000,
20 'public' : 'public',
21 'views' : 'views',
22 'view-engine' : 'jade',
23 'routes' : {},
24 'app' : express.createServer(),
25
26
27 //-- Initialization ------------------------------------------------//
28
29 initialize: function(params) {
30
31 params || (params = {});
32
33 this.routes = params['routes'] || this.routes;
34 this.port = params['port'] || this.port;
35
36 this.app.use(express.static(this.public));
37 this.app.set('view engine', this['view-engine']);
38 },
39
40
41 //-- Routing Methods Values ----------------------------------------//
42
43 route: function (method, path, action) {
44 this.routes[path] = [method, action]
45 },
46
47 get: function(path, action) {
48 this.route('get', path, action);
49 },
50
51 post: function(path, action) {
52 this.route('post', path, action);
53 },
54
55 put: function(path, action) {
56 this.route('put', path, action);
57 },
58
59 delete: function(path, action) {
60 this.route('delete', path, action);
61 },
62
63
64 //-- Server Methods ------------------------------------------------//
65
66 start: function() {
67
68 // Add routes
69 for (var i in this.routes) {
70
71 var method = this.routes[i][0],
72 action = this.routes[i][1];
73
74 console.log("Route created for " + i);
75 this.app[method](i, action);
76 }
77
78 this.app.listen(this.port);
79 console.log("Backbone Server is listening on port " + this.port);
80 }
81
82});
83
84
85module.exports = Backbone;
\No newline at end of file