Permalink
Browse files

Added res.show404().

  • Loading branch information...
twentyrogersc committed May 17, 2012
1 parent 9894d85 commit 7a0984016f9bc3b9e66d9e51e7a2ddc504ad4901
Showing with 41 additions and 2 deletions.
  1. +10 −0 README.md
  2. +9 −2 index.js
  3. +22 −0 test/teller.js
View
@@ -88,4 +88,14 @@ Redirect to another url. StatusCode is not required.
app.get(route, function(req, res) {
res.redirect('/login', statusCode)
})
+```
+
+### res.show404()
+
+Sends the default 404 page.
+
+```javascript
+app.get(route, function(req, res) {
+ res.show404()
+})
```
View
@@ -42,12 +42,12 @@ var addStaticRouting = function(opts) {
file = path.join(opts.dir, file)
if (path.existsSync(file) === false) {
- return res.end('404')
+ return res.show404()
}
if (fs.statSync(file).isDirectory()) {
file = file+'/index.html'
if (path.existsSync(file) === false) {
- return res.end('404')
+ return res.show404()
}
}
@@ -83,6 +83,12 @@ var render = function(template, data, code) {
this.end(html)
}
+var show404 = function() {
+ var head = { 'Content-Type': 'text/html' }
+ this.writeHead(404, head)
+ this.end('<h1>404, not found</h1>')
+}
+
var json = function(obj, code) {
var body = JSON.stringify(obj)
code = code === undefined ? 200 : code
@@ -102,6 +108,7 @@ var server = function(req, res) {
res.json = json
res.render = render
res.redirect = redirect
+ res.show404 = show404
req.url = url.parse(req.url)
var method = req.method.toLowerCase()
View
@@ -21,6 +21,9 @@ require('../index')
.get('/redirect', function(req, res) {
res.redirect('/render', req.query.code)
})
+ .get('/404', function(req, res) {
+ res.show404()
+ })
.post('/post', function(req, res) {
res.end('post')
})
@@ -116,6 +119,14 @@ describe('app', function() {
done()
})
})
+ it('should return a 404 if static file is not found', function(done) {
+ 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>')
+ done()
+ })
+ })
})
})
@@ -185,5 +196,16 @@ describe('res', function() {
})
})
})
+
+ describe('show404()', function() {
+ it('should show the default 404 page', function(done) {
+ 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>')
+ done()
+ })
+ })
+ })
})

0 comments on commit 7a09840

Please sign in to comment.