Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Using filed for serving static files.

  • Loading branch information...
commit 8e9db6efc04f7f23059aea2d98a3c25b98541861 1 parent 2c23956
@twentyrogersc authored
View
2  README.md
@@ -1,6 +1,6 @@
#teller
Simple node.js webserver.
-Dependencies: [crossroads](http://millermedeiros.github.com/crossroads.js/), [ejs](https://github.com/visionmedia/ejs), [mime](https://github.com/bentomas/node-mime), [formidable](https://github.com/felixge/node-formidable), [wrench](https://github.com/ryanmcgrath/wrench-js)
+Dependencies: [crossroads](http://millermedeiros.github.com/crossroads.js/), [ejs](https://github.com/visionmedia/ejs), [filed](https://github.com/mikeal/filed), [formidable](https://github.com/felixge/node-formidable), [wrench](https://github.com/ryanmcgrath/wrench-js)
[![Build Status](https://secure.travis-ci.org/twentyrogersc/teller.png)](http://travis-ci.org/twentyrogersc/teller)
View
64 index.js
@@ -1,7 +1,7 @@
var crossroads = require('crossroads')
var ejs = require('ejs')
+var filed = require('filed')
var formidable = require('formidable')
-var mime = require('mime')
var wrench = require('wrench')
var fs = require('fs')
@@ -27,49 +27,31 @@ routes.post.bypassed.add(bypassed)
app.settings = function(opts) {
for (var opt in opts) {
- switch (opt) {
- case 'static':
- addStaticRouting(opts[opt])
- break
- case 'template':
- cacheTemplates(opts[opt].dir)
- break
- }
+ if (opt in add) add[opt](opts[opt])
}
return app
}
-var addStaticRouting = function(opts) {
+var add = {}
+add.static = function(opts) {
var lastChar = opts.route.charAt(opts.route.length-1)
var route = lastChar !== '/' ? opts.route+'/' : opts.route
- var regex = new RegExp('^'+route.replace(/\//g, '\\/')+'(.{0,})$')
+ var routeExp = route+':file*:'
- routes.get.addRoute(regex, function(req, res) {
+ routes.get.addRoute(routeExp, function(req, res) {
var file = req.url.pathname.replace(route, '/')
file = path.join(opts.dir, file)
-
- if (path.existsSync(file) === false) {
- return res.show404()
- }
- if (fs.statSync(file).isDirectory()) {
- file = file+'/index.html'
- if (path.existsSync(file) === false) {
- return res.show404()
- }
- }
-
- var content = fs.readFileSync(file)
- res.writeHead(200, { 'Content-Type': mime.lookup(file) })
- res.end(content.toString())
+ if (path.existsSync(file)) filed(file).pipe(res)
+ else res.show404()
}, 1)
}
-var cacheTemplates = function(dir) {
- var files = wrench.readdirSyncRecursive(dir)
+add.template = function(opts) {
+ var files = wrench.readdirSyncRecursive(opts.dir)
files.forEach(function(file) {
- var p = path.join(dir, file)
+ var p = path.join(opts.dir, file)
var stats = fs.statSync(p)
- if ( stats.isDirectory() === false ) {
+ if (stats.isDirectory() === false) {
var content = fs.readFileSync(p)
cache[file] = content.toString()
}
@@ -84,24 +66,23 @@ var render = function(template, data, code) {
}
var html = ejs.render(cache[template], data)
- code = code === undefined ? 200 : code
- var head = { 'Content-Type': 'text/html' }
- this.writeHead(code, head)
- this.end(html)
+ this.send(html, code)
}
var show404 = function() {
- var head = { 'Content-Type': 'text/html' }
- this.writeHead(404, head)
- this.end('<h1>404, not found</h1>')
+ this.send('<h1>404, not found</h1>', 404)
}
var json = function(obj, code) {
var body = JSON.stringify(obj)
- code = code === undefined ? 200 : code
- var head = { 'Content-Type': 'application/json' }
- this.writeHead(code, head)
- this.end(body)
+ this.send(body, code, 'application/json')
+}
+
+var send = function(output, code, type) {
+ code = code || 200
+ type = type || 'text/html'
+ this.writeHead(code, { 'Content-Type': type })
+ this.end(String(output));
}
var redirect = function(url, code) {
@@ -115,6 +96,7 @@ var server = function(req, res) {
res.json = json
res.render = render
res.redirect = redirect
+ res.send = send
res.show404 = show404
req.url = url.parse(req.url)
View
2  package.json
@@ -11,7 +11,7 @@
"dependencies": {
"crossroads": "0.8.0",
"ejs": "0.7.1",
- "mime": "1.2.5",
+ "filed": "0.0.6",
"formidable": "1.0.9",
"wrench": "1.3.9"
},
View
0  test/static/static.txt → test/static/static.html
File renamed without changes
View
4 test/teller.js
@@ -118,10 +118,10 @@ describe('app', function() {
describe('settings()', function() {
it('should allow static file serving', function(done) {
- request('http://localhost:1234/public/static.txt', function(err, res, body) {
+ request('http://localhost:1234/public/static.html', function(err, res, body) {
should.not.exist(err)
should.exist(res)
- res.headers['content-type'].should.equal('text/plain')
+ res.headers['content-type'].should.equal('text/html')
res.statusCode.should.equal(200)
body.should.equal('static')
done()
Please sign in to comment.
Something went wrong with that request. Please try again.