Created
June 5, 2012 22:07
-
-
Save shimaore/2878394 to your computer and use it in GitHub Desktop.
express_3 first draft
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/package.json b/package.json | |
index 3d90697..665b891 100644 | |
--- a/package.json | |
+++ b/package.json | |
@@ -6,7 +6,7 @@ | |
"homepage": "http://zappajs.github.com/zappajs/", | |
"repository": {"type": "git", "url": "git://github.com/zappajs/zappajs.git"}, | |
"dependencies": { | |
- "express": "2.5.9", | |
+ "express": "3.0.x", | |
"socket.io": "0.9.x", | |
"coffeecup": "0.3.x", | |
"node-uuid": "1.2.0", | |
diff --git a/src/zappa.coffee b/src/zappa.coffee | |
index 44368cf..85135d6 100755 | |
--- a/src/zappa.coffee | |
+++ b/src/zappa.coffee | |
@@ -54,57 +54,70 @@ copy_data_to = (recipient, sources) -> | |
# Keep inline views at the module level and namespaced by app id | |
# so that the monkeypatched express can look them up. | |
views = {} | |
- | |
-# Monkeypatch express to support lookup of inline templates. Such is life. | |
-express.View.prototype.__defineGetter__ 'exists', -> | |
- # Path given by zappa: /path/to/appid/foo.bar. | |
- | |
- # Try appid/foo.bar in memory. | |
- p = @path.replace @root + '/', '' | |
- id = p.split('/')[0] | |
- return true if views[p] | |
- | |
- # Try appid/foo in memory. | |
- p = p.replace(path.extname(p), '') | |
- return true if views[p] | |
- # Try /path/to/foo.bar in filesystem (normal express behaviour). | |
- p = @path.replace id + '/', '' | |
- try | |
- fs.statSync(p) | |
- return true | |
- catch err | |
- p = @path | |
- try | |
- fs.statSync(p) | |
- return true | |
- catch err | |
- return false | |
- | |
-express.View.prototype.__defineGetter__ 'contents', -> | |
+# Monkeypatch express to support lookup of inline templates. Such is life. | |
+View = require '../node_modules/express/lib/view' | |
+Utils = require '../node_modules/express/lib/utils' | |
+ | |
+# This is a copy of express' lib/view.js , using our version of `exits`. | |
+View::lookup = (path) -> | |
+ ext = @ext | |
+ # <path>.<engine> | |
+ path = path.join(@root, path) if not Utils.isAbsolute path | |
+ found = @exists path | |
+ return found if found | |
+ | |
+ # <path>/index.<engine> | |
+ path = path.join dirname(path), basename(path,ext), 'index' + ext | |
+ found = @exists path | |
+ return found if found | |
+ | |
+build_engine = (content,ext) => | |
+ # For internal views we need to recode | |
+ # @engine = engines[ext] || (engines[ext] = require(ext.slice(1)).__express); | |
+ # Normally engines[ext] should have been added using | |
+ # app.engine zappa.adapter ... | |
+ # so they should already be `compile()` functions. | |
+ # However... `engines` is not accessible here, so just rely on the module. | |
+ # But: then this break zappa.adapter, since we're not longer using `engines`. | |
+ # Argggl! | |
+ | |
+ compile = require(ext.slice(1)).compile | |
+ template = compile content | |
+ (path,options,fn) -> | |
+ fn null, template options | |
+ | |
+View::exists = (location) -> | |
# Path given by zappa: /path/to/appid/foo.bar. | |
# Try appid/foo.bar in memory. | |
- p = @path.replace @root + '/', '' | |
+ p = location.replace @root + '/', '' | |
id = p.split('/')[0] | |
- return views[p] if views[p] | |
+ if views[p] | |
+ @engine = build_engine views[p], @ext | |
+ return p | |
# Try appid/foo in memory. | |
- p = p.replace(path.extname(p), '') | |
- return views[p] if views[p] | |
+ p = p.replace path.extname(p), '' | |
+ if views[p] | |
+ @engine = build_engine views[p], @ext | |
+ return p | |
# Try /path/to/foo.bar in filesystem (normal express behaviour). | |
- p = @path.replace id + '/', '' | |
- try | |
- fs.readFileSync p, 'utf8' | |
- catch err | |
- p = @path | |
- fs.readFileSync p, 'utf8' | |
+ p = location.replace id + '/', '' | |
+ exists = fs.existsSync or path.existsSync | |
+ if exists p | |
+ return p | |
+ | |
+ # Try the original path | |
+ if exists location | |
+ return location | |
+ false | |
# Takes in a function and builds express/socket.io apps based on the rules contained in it. | |
zappa.app = (func,options) -> | |
context = {id: uuid(), zappa, express} | |
- | |
+ | |
context.root = path.dirname(module.parent.filename) | |
# Storage for user-provided stuff. | |
@@ -113,7 +126,7 @@ zappa.app = (func,options) -> | |
helpers = {} | |
postrenders = {} | |
- app = context.app = express.createServer() | |
+ app = context.app = express() | |
io = if options.disable_io then null else context.io = socketio.listen(app) | |
# Reference to the zappa client, the value will be set later. | |
@@ -124,6 +137,7 @@ zappa.app = (func,options) -> | |
# Zappa's default settings. | |
app.set 'view engine', 'coffee' | |
+ # In Express 3, app.register is replaced by app.engine | |
app.register '.coffee', zappa.adapter require('coffeecup').adapters.express, | |
blacklist: ['format', 'autoescape', 'locals', 'hardcode', 'cache'] | |
@@ -517,7 +531,7 @@ zappa.run = -> | |
# Creates a zappa view adapter for templating engine `engine`. This adapter | |
# can be used with `app.register` and creates params "shortcuts". | |
-# | |
+# | |
# Zappa, by default, automatically sends all request params to templates, | |
# but inside the `params` local. | |
# |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment