Permalink
Browse files

remove express dependency

  • Loading branch information...
1 parent 21383f5 commit 17d8b2eaa24138526c40da5364f03c652be17477 @eldargab committed May 17, 2012
Showing with 33 additions and 19 deletions.
  1. +4 −4 lib/index.js
  2. +18 −10 lib/render.js
  3. +7 −1 package.json
  4. +4 −4 test/integration.js
View
@@ -18,10 +18,10 @@ module.exports = function (opts) {
}
function engine (ext, opts) {
- var cach = engine.cach
- var eng = cach[ext]
+ var cache = engine.cache
+ var eng = cache[ext]
if (eng === undefined) {
- eng = cach[ext] = opts[ext] || engines(ext) || null
+ eng = cache[ext] = opts[ext] || engines(ext) || null
if (eng) {
var extName = ext.substring(1)
mix(eng, {root: opts.root}, opts['*'], opts[extName])
@@ -30,7 +30,7 @@ function engine (ext, opts) {
return eng ? Object.create(eng) : null
}
-engine.cach = {}
+engine.cache = {}
module.exports.engine = engine
module.exports.engines = engines
View
@@ -1,27 +1,35 @@
var onerror = require('./util').nextOnENOENT
var extname = require('path').extname
var fs = require('fs')
+var mime = require('mime')
module.exports = function (Engine) {
return function render (file, req, res, next) {
var engine = Engine(extname(file))
if (engine && engine.renderFile) {
engine.renderFile(file, function (error, out) {
if (error) return onerror(error, next)
- res.set('Content-Type', engine.contentType)
- res.send(out)
+ send(out, engine.contentType)
})
- } else if (engine && engine.render) {
+ } else {
fs.readFile(file, 'utf8', function (error, string) {
if (error) return onerror(error, next)
- engine.render(string, function (error, out) {
- if (error) return next(error)
- res.set('Content-Type', engine.contentType)
- res.send(out)
- })
+ if (engine) {
+ engine.render(string, function (error, out) {
+ if (error) return next(error)
+ send(out, engine.contentType)
+ })
+ } else {
+ send(string, mime.lookup(file))
+ }
})
- } else {
- res.sendfile(file)
+ }
+
+ function send (str, contentType) {
+ res.charset = mime.charsets.lookup(contentType) || 'utf-8'
+ res.setHeader('Content-Type', contentType)
+ res.setHeader('Content-Length', Buffer.byteLength(str))
+ res.end(req.method == 'HEAD' ? null : str)
}
}
}
View
@@ -4,9 +4,15 @@
"description": "Serve your jades, markdowns, lesses like static files",
"version": "0.0.0",
"main": "lib/index",
+ "dependencies": {
+ "mime": "*"
+ },
"devDependencies": {
+ "mocha": "1.0.x",
+ "sinon": "1.3.x",
"less": "*",
"jade": "*",
- "markdown": "*"
+ "markdown": "*",
+ "stylus": "*"
}
}
View
@@ -19,23 +19,23 @@ describe('Integration tests', function () {
opts.less = {
paths: [opts.root + '/subdir']
}
- req.get('/').expect('text/css', /header/)
+ req.get('/').expect('text/css; charset=UTF-8', /header/)
})
testEngine('Stylus', function (req, opts) {
opts.styl = {
paths: [opts.root + '/subdir']
}
- req.get('/').expect('text/css', /header/)
+ req.get('/').expect('text/css; charset=UTF-8', /header/)
})
testEngine('Markdown', function (req) {
- req.get('/hello').expect('text/html', /<h1>Hello<\/h1>/)
+ req.get('/hello').expect('text/html; charset=UTF-8', /<h1>Hello<\/h1>/)
})
testEngine('Jade', function (req, opts) {
opts['*'] = {hello: 'hello'}
opts['jade'] = {world: 'world'}
- req.get('/hello').expect('text/html', /hello world/)
+ req.get('/hello').expect('text/html; charset=UTF-8', /hello world/)
})
})

0 comments on commit 17d8b2e

Please sign in to comment.