Reputation: 589
I am using harp as an Express middleware to render my static files, which I write as jade/coffee/stylus. Everything works fine, except when I browse a page that does not exist. Instead of the usual 404 page, I am greeted with a plain "Cannot GET /(page name)". If I run the site with "harp server" instead of using node, naturally it works fine.
I figured that the issue is that I have to redirect the 404 page from within the server. However, if I do that, Express does not recognise the jade format. I then have to install jade and use it as view engine. Plus, if I use stylus, I have to add that render as well. It defeats the purpose of using harp as middleware.
Can anybody suggest an alternative that handles custom 404.jade files just as in harp's standalone version?
My code:
express = require "express"
harp = require "harp"
app = express()
app.use express.static __dirname + "/public"
.use harp.mount __dirname + "/public"
#the following does not work
.use (req,res) ->
res.status(404).render __dirname + "/public/404.jade"
.listen 3000
Upvotes: 1
Views: 180
Reputation: 2854
It seems the problem lies here: https://github.com/sintaxi/harp/blob/master/lib/index.js#L89
exports.mount = function(mountPoint, root){
if(!root){
root = mountPoint
mountPoint = null
}else{
var rx = new RegExp("^" + mountPoint)
}
var finder = middleware.regProjectFinder(root)
return function(req, rsp, next){
if(rx){
if(!req.url.match(rx)) return next()
var originalUrl = req.url
req.url = req.url.replace(rx, "/")
}
finder(req, rsp, function(){
middleware.setup(req, rsp, function(){
middleware.static(req, rsp, function(){
middleware.poly(req, rsp, function(){
middleware.process(req, rsp, function(){
if(originalUrl) req.url = originalUrl
next() // HERE
})
})
})
})
})
}
}
Apparently, if no callback ends the response, harp simply restores the original url and calls next
, so the control goes back to your express app.
You could fork it and add a option to handle 404's, that is, let harp know that there will be no middlewares after it.
This line looks relevant: https://github.com/sintaxi/harp/blob/master/lib/middleware.js#L247
Upvotes: 1