express-resource
Advanced tools
Comparing version 0.2.4 to 1.0.0
require.paths.unshift(__dirname + '/../support'); | ||
/** | ||
* Module dependencies. | ||
*/ | ||
var express = require('express') | ||
, resource = require('../') | ||
, app = express.createServer(); | ||
, resource = require('..') | ||
, app = express(); | ||
var db = ['tobi', 'loki', 'jane'] | ||
, toys = ['ball', 'tunnel']; | ||
var users = app.resource('users', require('./controllers/user')); | ||
var pet = { | ||
index: { | ||
json: function(req, res){ | ||
res.send(db); | ||
}, | ||
default: function(req, res){ | ||
res.send(db.join(', '), { 'Content-Type': 'text/plain' }); | ||
} | ||
} | ||
}; | ||
var pets = app.resource('pets', pet); | ||
pets.load(function(id, fn){ | ||
fn(null, db[id]); | ||
app.get('/', function(req, res){ | ||
res.send('<a href="/users">View users</a>'); | ||
}); | ||
// GET /pets/toys.xml | ||
// this action must be defined above | ||
// the one below as the :pet placeholder | ||
// will otherwise match "/toys". | ||
pets.get('/toys', { | ||
xml: function(req, res){ | ||
res.send('<toys>' + toys.map(function(toy){ | ||
return '<toy>' + toy + '</toy>'; | ||
}).join('\n') + '</toys>'); | ||
} | ||
}); | ||
// GET /pets/1.xml | ||
pets.get({ | ||
xml: function(req, res){ | ||
res.send('<pet>' + req.pet + '</pet>'); | ||
} | ||
}); | ||
app.listen(3000); | ||
app.listen(3000); | ||
console.log('Listening on :3000'); |
1.0.0 / 2012-10-06 | ||
================== | ||
* add 3x support... Closes #55 | ||
0.2.4 / 2011-12-28 | ||
@@ -3,0 +8,0 @@ ================== |
51
index.js
/*! | ||
* Express - Resource | ||
* Copyright(c) 2010-2011 TJ Holowaychuk <tj@vision-media.ca> | ||
* Copyright(c) 2010-2012 TJ Holowaychuk <tj@vision-media.ca> | ||
* Copyright(c) 2011 Daniel Gasienica <daniel@gasienica.ch> | ||
@@ -14,3 +14,6 @@ * MIT Licensed | ||
var express = require('express') | ||
, methods = require('methods') | ||
, debug = require('debug')('express-resource') | ||
, lingo = require('lingo') | ||
, app = express.application | ||
, en = lingo.en; | ||
@@ -23,12 +26,18 @@ | ||
var orderedActions = [ | ||
'index' // GET / | ||
,'new' // GET /new | ||
,'create' // POST / | ||
,'show' // GET /:id | ||
,'edit' // GET /edit/:id | ||
,'update' // PUT /:id | ||
,'destroy' // DEL /:id | ||
'index' // GET / | ||
, 'new' // GET /new | ||
, 'create' // POST / | ||
, 'show' // GET /:id | ||
, 'edit' // GET /edit/:id | ||
, 'update' // PUT /:id | ||
, 'destroy' // DEL /:id | ||
]; | ||
/** | ||
* Expose `Resource`. | ||
*/ | ||
module.exports = Resource; | ||
/** | ||
* Initialize a new `Resource` with the given `name` and `actions`. | ||
@@ -42,3 +51,3 @@ * | ||
var Resource = module.exports = function Resource(name, actions, app) { | ||
function Resource(name, actions, app) { | ||
this.name = name; | ||
@@ -55,3 +64,3 @@ this.app = app; | ||
// default actions | ||
for (var i=0, key; i < orderedActions.length; i++) { | ||
for (var i = 0, key; i < orderedActions.length; ++i) { | ||
key = orderedActions[i]; | ||
@@ -150,10 +159,8 @@ if (actions[key]) this.mapDefaultAction(key, actions[key]); | ||
req.format = req.params.format || req.format || self.format; | ||
if (req.format) res.contentType(req.format); | ||
if (req.format) res.type(req.format); | ||
if ('object' == typeof fn) { | ||
if (req.format && fn[req.format]) { | ||
if (fn[req.format]) { | ||
fn[req.format](req, res, next); | ||
} else if (fn.default) { | ||
fn.default(req, res, next); | ||
} else { | ||
res.send(406); | ||
res.format(fn); | ||
} | ||
@@ -193,3 +200,8 @@ } else { | ||
delete routes[key]; | ||
app[method](key).remove(); | ||
if (method == 'del') method = 'delete'; | ||
app.routes[method].forEach(function(route, i){ | ||
if (route.path == key) { | ||
app.routes[method].splice(i, 1); | ||
} | ||
}) | ||
resource.map(route.method, route.orig, route.fn); | ||
@@ -240,3 +252,3 @@ } | ||
express.router.methods.concat(['del', 'all']).forEach(function(method){ | ||
methods.concat(['del', 'all']).forEach(function(method){ | ||
Resource.prototype[method] = function(path, fn){ | ||
@@ -259,4 +271,3 @@ if ('function' == typeof path | ||
express.HTTPServer.prototype.resource = | ||
express.HTTPSServer.prototype.resource = function(name, actions, opts){ | ||
app.resource = function(name, actions, opts){ | ||
var options = actions || {}; | ||
@@ -263,0 +274,0 @@ if ('object' == typeof name) actions = name, name = null; |
@@ -1,20 +0,36 @@ | ||
{ "name": "express-resource" | ||
, "description": "Resourceful routing for express" | ||
, "version": "0.2.4" | ||
, "author": "TJ Holowaychuk <tj@vision-media.ca>" | ||
, "contributors": [ | ||
{ "name": "Daniel Gasienica", "email": "daniel@gasienica.ch" } | ||
] | ||
, "dependencies": { "lingo": ">= 0.0.4" } | ||
, "devDependencies": { | ||
"connect": "1.8.x" | ||
, "express": "2.5.x" | ||
, "ejs": "0.4.x" | ||
, "expresso": "0.9.x" | ||
, "qs": "0.1.x" | ||
, "should": "*" | ||
{ | ||
"name": "express-resource", | ||
"description": "Resourceful routing for express", | ||
"version": "1.0.0", | ||
"homepage": "https://github.com/visionmedia/express-resource", | ||
"author": "TJ Holowaychuk <tj@vision-media.ca>", | ||
"contributors": [ | ||
{ | ||
"name": "Daniel Gasienica", | ||
"email": "daniel@gasienica.ch" | ||
} | ||
], | ||
"dependencies": { | ||
"lingo": ">= 0.0.4", | ||
"methods": "0.0.1", | ||
"debug": "*" | ||
}, | ||
"devDependencies": { | ||
"connect": "2.x", | ||
"express": "3.x", | ||
"ejs": "0.4.x", | ||
"qs": "0.1.x", | ||
"mocha": "*", | ||
"should": "*", | ||
"supertest": "0.1.2" | ||
}, | ||
"keywords": [ | ||
"express", | ||
"rest", | ||
"resource" | ||
], | ||
"main": "index", | ||
"engines": { | ||
"node": "*" | ||
} | ||
, "keywords": ["express", "rest", "resource"] | ||
, "main": "index" | ||
, "engines": { "node": ">= 0.2.0" } | ||
} | ||
} |
# Express Resource | ||
express-resource provides resourceful routing to express. | ||
express-resource provides resourceful routing to express. For Express 2.x | ||
use a version __below__ 1.0, for Express 3.x use 1.x. | ||
@@ -174,3 +175,3 @@ ## Installation | ||
Copyright (c) 2010-2011 TJ Holowaychuk <tj@vision-media.ca> | ||
Copyright (c) 2010-2012 TJ Holowaychuk <tj@vision-media.ca> | ||
Copyright (c) 2011 Daniel Gasienica <daniel@gasienica.ch> | ||
@@ -177,0 +178,0 @@ |
Sorry, the diff of this file is not supported yet
Wildcard dependency
QualityPackage has a dependency with a floating version range. This can cause issues if the dependency publishes a new major version.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
0
196
18484
3
7
13
353
2
+ Addeddebug@*
+ Addedmethods@0.0.1
+ Addeddebug@4.3.7(transitive)
+ Addedmethods@0.0.1(transitive)
+ Addedms@2.1.3(transitive)