Skip to content
Browse files

Prepare for release

  • Loading branch information...
1 parent c039dcf commit d444b6f84f24ab775aa2d81c021ca0f6b03b95f6 @eldargab committed
Showing with 83 additions and 5 deletions.
  1. +2 −0 .npmignore
  2. +66 −2 README.md
  3. +6 −2 lib/index.js
  4. +9 −1 package.json
View
2 .npmignore
@@ -0,0 +1,2 @@
+test/
+README.md
View
68 README.md
@@ -1,8 +1,10 @@
# Connect views
This is a connect middleware. It serves templates like a static files, supports
-request paths with omited extensions (`some/doc` vs `some/doc.md`), has built in
-support for jade, markdown, less and stylus.
+request paths with omited extensions, has built in support for
+[jade](http://jade-lang.com/),
+[markdown](https://github.com/evilstreak/markdown-js),
+[less](http://lesscss.org/) and [stylus](http://learnboost.github.com/stylus/).
## Examples
@@ -69,6 +71,68 @@ app.use(Views({
}))
```
+## Customization
+
+Several request processing steps are distinguished within module:
+
+1. Translate request's url into a file system path
+2. Lookup actual file to render
+3. Create view engine instance
+4. Render and send file contents
+
+For each step there is an independent public API component doing that job. You
+can always plug your own implementation for any step by monkey patching or by
+passing corresponding option to `Views()`:
+
+``` javascript
+// Serve only strict paths
+app.use(Views({
+ pathHandler: function (path, req, res, next) {
+ var opts = this
+
+ fs.stat(path, function (error, stat) {
+ if (error && error.code == 'ENOENT') return next()
+ if (error) return next(error)
+ opts.render(path, req, res, next)
+ })
+ }
+}))
+```
+
+## View engine
+
+View engine is an object with either `render(str, cb)` or `renderFile(path, cb)`
+method available. Engines which are just plain text processors should use
+`render` method, while engines which are able to bound several documents
+together should use `renderFile`. Such convention is useful as we can rely on
+file modification times for conditional gets and caching if there is only
+`render` method.
+
+There are also other conventions:
+
+* `contentType` property
+
+* All options are passed via `this`
+
+* By default a new engine instance is created for each request
+( with `Object.create(cachedProto)`). So it is safe to pass per request options.
+
+
+## Installation
+
+Via npm:
+
+``` bash
+$ npm install connect-views
+```
+
+To run tests install dev dependencies and launch npm test command:
+
+``` bash
+$ npm install -d
+$ npm test
+```
+
## License
(The MIT License)
View
8 lib/index.js
@@ -15,9 +15,13 @@ function Views (opts) {
return Views.engine(ext, opts)
}
- opts.pathHandler = opts.pathHandler || Views.PathLookup(Views.Render(function (ext) {
+ opts.render = opts.render || Views.Render(function (ext) {
return opts.engine(ext)
- }))
+ })
+
+ opts.pathHandler = opts.pathHandler || Views.PathLookup(function (path, req, res, next) {
+ opts.render(path, req, res, next)
+ })
return Views.Middleware(opts.root, function (path, req, res, next) {
opts.pathHandler(path, req, res, next)
View
10 package.json
@@ -1,13 +1,21 @@
{
"author": "Eldar Gabdullin <eldargab@gmail.com>",
"name": "connect-views",
- "description": "Serve your jades, markdowns, lesses like static files",
+ "description": "Serve your jades, markdowns, lesses like a static files",
"version": "0.0.0",
"main": "lib/index",
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/eldargab/connect-views.git"
+ },
+ "scripts": {
+ "test": "node_modules/.bin/mocha -R spec"
+ },
"dependencies": {
"mime": "*"
},
"devDependencies": {
+ "express": "3.0.x",
"mocha": "1.0.x",
"sinon": "1.3.x",
"less": "*",

0 comments on commit d444b6f

Please sign in to comment.
Something went wrong with that request. Please try again.