backbone-server
Advanced tools
Comparing version 0.2.2 to 0.3.0
@@ -1,19 +0,57 @@ | ||
// Example Server | ||
// Name : Example Server | ||
// Author : Nate Hunzaker | ||
// | ||
//-- 1. Create Backbone.Server | ||
//-- 2. Collect Data | ||
//-- 3. Routes | ||
//-- 4. Socket.io Methods | ||
//-- 5. Start Server | ||
// | ||
// Require backbone | ||
var Backbone = require('../lib/backbone-server'); | ||
//-- 1. Create Backbone.Server | ||
var Backbone = require('../lib/backbone-server'); | ||
var server = new Backbone.Server({ | ||
'port' : 9000, | ||
'socketio' : true, | ||
'view engine' : 'ejs' | ||
}); | ||
// Create a new server instance | ||
var server = new Backbone.Server(); | ||
//-- 2. Collect Data | ||
var articles = require('./data.js').article_data; | ||
// Add routes | ||
server.get('/', function(req, res) { | ||
res.send("Hello, world!"); | ||
}); | ||
//-- 3. Routes | ||
// Start server | ||
server.start(); | ||
// Index | ||
server.get('/', function(req, res) { | ||
res.render("index"); | ||
}); | ||
// Single Article Page | ||
server.get('/:name', function(req, res) { | ||
var article = articles.get(req.params.name).toJSON(); | ||
res.render('article', article); | ||
}); | ||
// For partial rendering on the client side | ||
server.get('/template/:name', function(req, res) { | ||
res.sendfile(__dirname + '/views/' + req.params.name + "." + server['view engine']); | ||
}); | ||
//-- 4. Socket.io Functions | ||
server.receive('connection', function() { | ||
console.log("Received connection, sending articles..."); | ||
server.send('articles', articles); | ||
}); | ||
//-- 5. Start Server | ||
server.start(); |
@@ -9,5 +9,9 @@ // Backbone Server | ||
var Backbone = require('backbone'); | ||
express = require('express'); | ||
var Underscore = require('underscore'); | ||
Backbone = require('backbone'); | ||
express = require('express'), | ||
ejs = require('ejs'); | ||
fs = require('fs'); | ||
var _ = Underscore; | ||
@@ -23,5 +27,6 @@ //-- Backbone Server ---------------------------------------------------// | ||
'views' : 'views', | ||
'view-engine' : 'jade', | ||
'view engine' : 'jade', | ||
'routes' : {}, | ||
'app' : express.createServer(), | ||
'express' : express.createServer(), | ||
'socketio' : false, | ||
@@ -33,13 +38,29 @@ | ||
params || (params = {}); | ||
// Merge in custom properties | ||
_.extend(this, params); | ||
this.routes = params['routes'] || this.routes; | ||
this.port = params['port'] || this.port; | ||
// Set static content folder | ||
this.express.use(express.static(this.public)); | ||
this.app.use(express.static(this.public)); | ||
this.app.set('view engine', this['view-engine']); | ||
// Set the view engine | ||
this.express.set('view engine', this['view engine']); | ||
//-- SocketIO Methods -----------------------------------------// | ||
if ( this['socketio'] ) { | ||
this.io = require('socket.io').listen(this.express); | ||
// A syntax sugar for socketIO | ||
this.receive = function(name, action) { | ||
this.io.sockets.on(name, action); | ||
} | ||
this.send = function(name, action) { | ||
this.io.sockets.emit(name, action); | ||
} | ||
} | ||
}, | ||
//-- Routing Methods Values ----------------------------------------// | ||
//-- Standard CRUD Routing Methods --------------------------------// | ||
@@ -70,3 +91,3 @@ route: function (method, path, action) { | ||
start: function() { | ||
// Add routes | ||
@@ -78,7 +99,20 @@ for (var i in this.routes) { | ||
console.log("Route created for " + i); | ||
this.app[method](i, action); | ||
this.express[method](i, action); | ||
} | ||
this.app.listen(this.port); | ||
// Add a route to load backbone: | ||
this.express.get('/backbone-server/backbone.js', function(req, res) { | ||
console.log('hi'); | ||
var underscorejs = fs.readFileSync(__dirname + '/node_modules/underscore/underscore-min.js', 'utf-8'), | ||
backbonejs = fs.readFileSync(__dirname + '/node_modules/backbone/backbone.js', 'utf-8'); | ||
res.contentType('text/javascript'); | ||
res.send(underscorejs + "\n" + backbonejs); | ||
}); | ||
this.express.listen(this.port); | ||
console.log("Backbone Server is listening on port " + this.port); | ||
@@ -85,0 +119,0 @@ } |
{ | ||
"author": "Nate Hunzaker <nate.hunzaker@gmail.com> (natehunzaker.com)", | ||
"name": "backbone-server", | ||
"description": "An interface between Backbone and Express. Creates a Backbone.Server object.", | ||
"version": "0.2.2", | ||
"description": "Creates a Backbone.Server object which interfaces between Backbone, Socket.IO and Express.", | ||
"version": "0.3.0", | ||
"main": "./lib/backbone-server.js", | ||
@@ -17,4 +17,7 @@ "repository": { | ||
"dependencies": { | ||
"express": ">= 2.4.6", | ||
"backbone": ">= 0.5.3" | ||
"underscore" : "*", | ||
"backbone" : "*", | ||
"express" : "*", | ||
"socket.io" : "*", | ||
"ejs" : "*" | ||
}, | ||
@@ -21,0 +24,0 @@ |
@@ -18,2 +18,3 @@ # Backbone-Server | ||
'routes' : {}, // Any initial routes you may have (or create them all at once) | ||
'socketio' : false // If set to true, Backbone.Server will enable socket.io functionality | ||
}); | ||
@@ -30,3 +31,3 @@ ``` | ||
}); | ||
`` | ||
``` | ||
@@ -60,2 +61,64 @@ ## Routes | ||
``` | ||
## Using Socket.io functions: | ||
To enable socket.io functionality, set the `socketio` attribute to `true` upon creation of your new Backbone.Server object: | ||
``` | ||
var server = new Backbone.Server( | ||
'socketio' : true // If set to true, Backbone.Server will enable socket.io functionality | ||
}); | ||
``` | ||
This will create an instance of socket.io at the `io` attribute of your new server object (`server.io`). You can now add standard socket.io events using the `send` and `receive` methods, like so: | ||
``` | ||
// Socket.IO Functions | ||
server.receive('connection', function() { | ||
console.log("connected"); | ||
//... enter your server side code to create your models | ||
server.send('models', [a, b, c, d]); | ||
}); | ||
``` | ||
In your clientside application, you can receive commands from the server like so (note that backbone-server automatically | ||
provides a route to load Underscore and Backbone): | ||
``` | ||
<script src="/backbone-server/backbone.js" type="text/javascript"></script> | ||
<script src="/socket.io/socket.io.js" type="text/javascript"></script> | ||
<script> | ||
var socket = io.connect('http://localhost'); | ||
var Article = Backbone.Model.extend(); | ||
var Articles = Backbone.Collection.extend({ | ||
model: Article, | ||
initialize: function() { | ||
// Bind an event to notify when an article has been added to the server | ||
this.bind('add', function(article) { | ||
console.log("Article added!"); | ||
}); | ||
} | ||
}); | ||
var articles = new Articles(); | ||
socket.on('models', function(data) { | ||
_.each(data, function(d) { | ||
articles.add(d); // queues the collection event ('articles added!'); | ||
}); | ||
}); | ||
</script> | ||
``` | ||
To summarize, in the example above, we've created a `Backbone.Server` which will push down server side models upon connection, and add them | ||
to the client scripts `Articles` collection. | ||
## Starting the Server: | ||
@@ -67,6 +130,16 @@ | ||
## Don't see something here you like from Express? | ||
## Don't see something here you like from Express or Socket.io? | ||
Target Express directly: | ||
###Target Express directly: | ||
`server.app // === Express ` | ||
``` | ||
// server.express == Express | ||
server.express | ||
``` | ||
###Target Socket.io directly: | ||
``` | ||
// server.io == socket.io | ||
server.io | ||
``` |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Install scripts
Supply chain riskInstall scripts are run when the package is installed. The majority of malware in npm is hidden in install scripts.
Found 1 instance in 1 package
Wildcard dependency
QualityPackage has a dependency with a floating version range. This can cause issues if the dependency publishes a new major version.
Found 5 instances in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7241144
223
30034
141
5
1
3
6
2
33
22
+ Addedejs@*
+ Addedsocket.io@*
+ Addedunderscore@*
+ Added@socket.io/component-emitter@3.1.2(transitive)
+ Added@types/cookie@0.4.1(transitive)
+ Added@types/cors@2.8.17(transitive)
+ Added@types/node@22.10.1(transitive)
+ Addedaccepts@1.3.8(transitive)
+ Addedansi-styles@4.3.0(transitive)
+ Addedasync@3.2.6(transitive)
+ Addedbalanced-match@1.0.2(transitive)
+ Addedbase64id@2.0.0(transitive)
+ Addedbrace-expansion@1.1.112.0.1(transitive)
+ Addedchalk@4.1.2(transitive)
+ Addedcolor-convert@2.0.1(transitive)
+ Addedcolor-name@1.1.4(transitive)
+ Addedconcat-map@0.0.1(transitive)
+ Addedcookie@0.7.2(transitive)
+ Addedcors@2.8.5(transitive)
+ Addedejs@3.1.10(transitive)
+ Addedengine.io@6.6.2(transitive)
+ Addedengine.io-parser@5.2.3(transitive)
+ Addedfilelist@1.0.4(transitive)
+ Addedhas-flag@4.0.0(transitive)
+ Addedjake@10.9.2(transitive)
+ Addedminimatch@3.1.25.1.6(transitive)
+ Addednegotiator@0.6.3(transitive)
+ Addedobject-assign@4.1.1(transitive)
+ Addedsocket.io@4.8.1(transitive)
+ Addedsocket.io-adapter@2.5.5(transitive)
+ Addedsocket.io-parser@4.2.4(transitive)
+ Addedsupports-color@7.2.0(transitive)
+ Addedundici-types@6.20.0(transitive)
+ Addedws@8.17.1(transitive)
Updatedbackbone@*
Updatedexpress@*