Skip to content

Commit

Permalink
refactors fallback 404. Closes#88
Browse files Browse the repository at this point in the history
  • Loading branch information
sintaxi committed Oct 10, 2013
1 parent 350178b commit 778af6e
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 8 deletions.
8 changes: 2 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ exports.server = function(dirPath, options, callback){
middleware.static,
middleware.poly,
middleware.process,
middleware.staticNotFound,
middleware.dynamicNotFound,
middleware.devFallbackNotFound
middleware.fallback
).listen(options.port || 9966, callback)
}

Expand All @@ -53,9 +51,7 @@ exports.multihost = function(dirPath, options, callback){
middleware.static,
middleware.poly,
middleware.process,
middleware.staticNotFound,
middleware.dynamicNotFound,
middleware.devFallbackNotFound
middleware.fallback
).listen(options.port || 9966, callback)
}

Expand Down
8 changes: 6 additions & 2 deletions lib/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ exports.hostProjectFinder = function(dirPath){
req.projectPath = path.resolve(dirPath, matches[0])
next()
}else{
// TODO: add better error message here
rsp.end("Cannot find project")
}

Expand Down Expand Up @@ -236,7 +237,10 @@ var default404 = function(req, rsp, next){
layout: "_layout.jade"
}
polymer.root(__dirname + "/templates").render("404.jade", locals, function(err, body){
rsp.statusCode = 500
var type = helpers.mimeType("html")
var charset = mime.charsets.lookup(type)
rsp.setHeader('Content-Type', type + (charset ? '; charset=' + charset : ''));
rsp.statusCode = 404
rsp.end(body)
})
}
Expand Down Expand Up @@ -464,7 +468,7 @@ exports.devFallbackNotFound = function(req, rsp){
layout: "_layout.jade"
}
polymer.root(__dirname + "/templates").render("404.jade", locals, function(err, body){
rsp.status = 500
rsp.status = 404
rsp.end(body)
})
}
10 changes: 10 additions & 0 deletions test/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ describe("basic", function(){
var agent = superagent.agent()
agent.get('http://localhost:8100/some/missing/path').end(function(err, rsp){
rsp.should.have.status(404)
rsp.headers.should.have.property("content-type", "text/html; charset=UTF-8")
rsp.text.should.not.include("Kitchen Sink")
rsp.text.should.include("Custom, Page Not Found")
rsp.text.should.eql(contents.toString())
Expand All @@ -80,6 +81,15 @@ describe("basic", function(){
})
})

it("should return proper mime type on 404 page", function(done){
var agent = superagent.agent()
agent.get('http://localhost:8100/some/missing/path.css').end(function(err, rsp){
rsp.should.have.status(404)
rsp.headers.should.have.property("content-type", "text/html; charset=UTF-8")
done()
})
})

it("should render HTML page without requiring extension", function(done){
fs.readFile(path.join(outputPath, "basic.html"), function(err, contents){
contents.toString().should.not.include("Kitchen Sink")
Expand Down
21 changes: 21 additions & 0 deletions test/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,27 @@ describe("errors", function(){
})
})

describe("err-missing-public", function(){
var projectPath = path.join(__dirname, "apps/err-missing-404")
var outputPath = path.join(__dirname, "out/err-missing-404")
var port = 8114

before(function(done){
harp.server(projectPath, { port: port }, function(){
done()
})
})

it("should return proper mime type on 404 page", function(done){
var agent = superagent.agent()
agent.get('http://localhost:'+ port +'/some/missing/path.css').end(function(err, rsp){
rsp.should.have.status(404)
rsp.headers.should.have.property("content-type", "text/html; charset=UTF-8")
done()
})
})
})

after(function(done){
exec("rm -rf " + path.join(__dirname, "out"), function(){
done()
Expand Down

0 comments on commit 778af6e

Please sign in to comment.