Permalink
Browse files

Allow custom 404 pages.

  • Loading branch information...
1 parent 47069df commit e6c2ef02fe8f934e1c7f4f1f26cee1f9a7f2d5cb @twentyrogersc committed Aug 2, 2012
Showing with 25 additions and 14 deletions.
  1. +3 −3 README.md
  2. +6 −3 index.js
  3. +1 −1 package.json
  4. +1 −0 test/404.ejs
  5. +14 −7 test/teller.js
View
@@ -57,7 +57,7 @@ Settings for template rendering and static file serving.
```javascript
app.settings({
- template: { dir: __dirname+'/templates' },
+ template: { dir: __dirname+'/templates', 404: '404.ejs' },
static: { route: '/public', dir: __dirname+'/public' }
})
```
@@ -151,10 +151,10 @@ app.get(route, function(req, res) {
### res.show404()
-Sends the default 404 page.
+If ```settings.template['404']``` is supplied, renders a custom 404 page, otherwise sends the default 404 page.
```javascript
app.get(route, function(req, res) {
- res.show404()
+ res.show404(data)
})
```
View
@@ -12,6 +12,7 @@ var url = require('url')
var app = {}
var cache = {}
+var notFound = false
app.settings = function(opts) {
@@ -34,6 +35,7 @@ add.static = function(opts) {
}
add.template = function(opts) {
+ notFound = opts['404']
var files = wrench.readdirSyncRecursive(opts.dir)
files.forEach(function(file) {
var p = path.join(opts.dir, file)
@@ -42,7 +44,7 @@ add.template = function(opts) {
var content = fs.readFileSync(p)
cache[file] = content.toString()
}
- })
+ })
}
@@ -55,8 +57,9 @@ var render = function(template, data, code) {
this.send(html, code)
}
-var show404 = function() {
- this.send('<h1>404, not found</h1>', 404)
+var show404 = function(data) {
+ if (notFound) this.render(notFound, data, 404)
+ else this.send('<h1>404, not found</h1>', 404)
}
crossroads.bypassed.add(function(req, res) {
View
@@ -1,7 +1,7 @@
{
"name": "teller",
"description" : "Simple node.js webserver.",
- "version" : "0.8.0",
+ "version" : "0.9.0",
"main": "./index.js",
"repository": {
"type": "git",
View
@@ -0,0 +1 @@
+<h1><%= 'custom' %></h1>
View
@@ -46,7 +46,7 @@ require('../index')
res.json(req.route)
})
.settings({
- template: { dir: __dirname },
+ template: { dir: __dirname, 404: '404.ejs' },
static: { route: '/public', dir: __dirname+'/static' }
})
.listen(1234)
@@ -86,7 +86,7 @@ describe('app', function() {
should.not.exist(err)
res.headers['content-type'].should.equal('text/html')
res.statusCode.should.equal(404)
- body.should.equal('<h1>404, not found</h1>')
+ // body.should.equal('<h1>404, not found</h1>')
done()
})
})
@@ -136,7 +136,7 @@ describe('app', function() {
}, function(err, res, body) {
res.headers['content-type'].should.equal('text/html')
res.statusCode.should.equal(404)
- body.should.equal('<h1>404, not found</h1>')
+ // body.should.equal('<h1>404, not found</h1>')
done()
})
})
@@ -186,7 +186,7 @@ describe('app', function() {
}, function(err, res, body) {
res.headers['content-type'].should.equal('text/html')
res.statusCode.should.equal(404)
- body.should.equal('<h1>404, not found</h1>')
+ // body.should.equal('<h1>404, not found</h1>')
done()
})
})
@@ -207,12 +207,19 @@ describe('app', function() {
request('http://localhost:1234/public/static.tx', function(err, res, body) {
res.headers['content-type'].should.equal('text/html')
res.statusCode.should.equal(404)
- body.should.equal('<h1>404, not found</h1>')
+ // body.should.equal('<h1>404, not found</h1>')
+ done()
+ })
+ })
+ it('should return a custom 404 if opts["404"] is supplied', function(done) {
+ request('http://localhost:1234/does/not/exist', function(err, res, body) {
+ res.headers['content-type'].should.equal('text/html')
+ res.statusCode.should.equal(404)
+ body.should.equal('<h1>custom</h1>')
done()
})
})
})
-
})
describe('res', function() {
@@ -297,7 +304,7 @@ describe('res', function() {
request('http://localhost:1234/404', function(err, res, body) {
res.headers['content-type'].should.equal('text/html')
res.statusCode.should.equal(404)
- body.should.equal('<h1>404, not found</h1>')
+ // body.should.equal('<h1>404, not found</h1>')
done()
})
})

0 comments on commit e6c2ef0

Please sign in to comment.