Skip to content
Browse files

Added static file serving.

  • Loading branch information...
1 parent 8bbb6a8 commit 77b87fb597ee0903d7ed48aa083495e25ad854ae @twentyrogersc committed May 16, 2012
Showing with 82 additions and 16 deletions.
  1. +16 −5 README.md
  2. +39 −5 index.js
  3. +3 −2 package.json
  4. BIN test/static/.DS_Store
  5. +1 −0 test/static/static.txt
  6. +23 −4 test/teller.js
View
21 README.md
@@ -38,6 +38,17 @@ app.post(route, function(req, res) {
})
```
+### app.settings()
+
+Settings for template rendering and static file serving.
+
+```javascript
+app.settings({
+ template: { dir: __dirname+'/templates' },
+ static: { route: '/public', dir: __dirname+'/public' }
+})
+```
+
### app.listen()
Begin accepting connections on the specified port.
@@ -48,18 +59,18 @@ app.listen(1234)
## Response
-### res.render
+### res.render()
-Render the specified template. In order to call this, app.setTemplateDir() is required to set the directory of the templates. Data and statusCode are not required.
+Render the specified template, the path of which must be specified in app.settings(). Data and statusCode are not required.
```javascript
-app.setTemplateDir(__dirname)
+app.settings({ dir: __dirname+'/templates' })
app.get(route, function(req, res) {
res.render('template.ejs', data, statusCode)
})
```
-### res.json
+### res.json()
Render the object as json. StatusCode is not required.
@@ -69,7 +80,7 @@ app.get(route, function(req, res) {
})
```
-### res.redirect
+### res.redirect()
Redirect to another url. StatusCode is not required.
View
44 index.js
@@ -1,27 +1,61 @@
var crossroads = require('crossroads')
var ejs = require('ejs')
var formidable = require('formidable')
+var static = require('node-static')
var fs = require('fs')
var http = require('http')
var qs = require('querystring')
var url = require('url')
var app = {}
-var dir = '/'
+var settings = {}
var routes = {
get: crossroads.create(),
post: crossroads.create()
}
-app.setTemplateDir = function(newDir) {
- dir = newDir
+
+app.settings = function(opts) {
+ for (var opt in opts) {
+ switch(opt) {
+ case 'template':
+ case 'static':
+ add[opt](opts[opt])
+ break
+ }
+ }
return app
-}
+}
+
+var add = {
+ static: function(opts) {
+ var file = new static.Server(opts.dir)
+
+ var last = opts.route.charAt(opts.route.length-1)
+ var regex = last !== '/' ? opts.route+'/' : opts.route
+ regex = '^'+regex.replace(/\//g, '\\/')+'(.{0,})$'
+ regex = new RegExp(regex)
+
+ var staticCb = function(req, res) {
+ var path = req.url.pathname.match(regex)[1]
+ req.addListener('end', function() {
+ file.serveFile(path, 400, {}, req, res)
+ })
+ }
+
+ routes.get.addRoute(regex, staticCb, 1)
+ routes.post.addRoute(regex, staticCb, 1)
+ },
+ template: function(opts) {
+ settings.template = opts.dir
+ }
+}
+
var render = function(template, data, code) {
var res = this
- template = [dir, template].join('/')
+ template = [settings.template, template].join('/')
fs.readFile(template, function (err, buff) {
if (err) throw err
var html = ejs.render(buff.toString(), data)
View
5 package.json
@@ -1,7 +1,7 @@
{
"name": "teller",
"description" : "Basic node.js webserver.",
- "version" : "0.1.0",
+ "version" : "0.2.0",
"main": "./index.js",
"repository": {
"type": "git",
@@ -11,7 +11,8 @@
"dependencies": {
"crossroads": "0.8.0",
"ejs": "0.7.1",
- "formidable": "1.0.9"
+ "formidable": "1.0.9",
+ "node-static": "0.5.9"
},
"devDependencies": {
"mocha": "*",
View
BIN test/static/.DS_Store
Binary file not shown.
View
1 test/static/static.txt
@@ -0,0 +1 @@
+static
View
27 test/teller.js
@@ -30,7 +30,10 @@ require('../index')
.post('/body', function(req, res) {
res.end(req.body.a)
})
- .setTemplateDir(__dirname)
+ .settings({
+ template: { dir: __dirname },
+ static: { route: '/public', dir: __dirname+'/static' }
+ })
.listen(1234)
@@ -102,7 +105,23 @@ describe('app', function() {
})
})
- describe('res.json()', function() {
+ describe('settings()', function() {
+ it('should allow static file serving', function(done) {
+ request('http://localhost:1234/public/static.txt', function(err, res, body) {
+ should.not.exist(err)
+ should.exist(res)
+ res.headers['content-type'].should.equal('text/plain')
+ body.should.equal('static')
+ done()
+ })
+ })
+ })
+
+})
+
+describe('res', function() {
+
+ describe('json()', function() {
it('should respond with json', function(done) {
request('http://localhost:1234/json?code=200', function(err, res, body) {
should.not.exist(err)
@@ -123,7 +142,7 @@ describe('app', function() {
})
})
- describe('res.render()', function() {
+ describe('render()', function() {
it('should respond with rendered html', function(done) {
request('http://localhost:1234/render?code=200', function(err, res, body) {
should.not.exist(err)
@@ -144,7 +163,7 @@ describe('app', function() {
})
})
- describe('res.redirect()', function() {
+ describe('redirect()', function() {
it('should redirect to to correct page', function(done) {
request('http://localhost:1234/redirect', function(err, res) {
should.not.exist(err)

0 comments on commit 77b87fb

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