Permalink
Browse files

add 404 to FAQ

  • Loading branch information...
1 parent 3fa5c1a commit a4ef9253a6f3cedb8614ae8ceec6966d65530cd3 @tj tj committed Sep 13, 2012
Showing with 28 additions and 1 deletion.
  1. +17 −0 en/faq.jade
  2. +11 −1 faq.html
View
@@ -90,6 +90,23 @@ setup
wiki page, or view a <a href="https://github.com/visionmedia/express/wiki/New-features-in-3.x">list of changes</a> made in 3.x.
setup
+ h3(id='404-handling') How do you handle 404s?
+
+ p.
+ In Express 404s are not the result of an error, thus the
+ error-handler middleware will not capture 404s, this is
+ because a 404 is simply the absence of additional work to do,
+ in other words Express has executed all middleware / routes
+ and found that none of them responded. All you need to
+ do is add a middleware at the very bottom below all the others
+ to handle a 404:
+
+ +js.
+ app.use(function(req, res, next){
+ res.send(404, 'Sorry cant find that!');
+ });
+
+setup
h3(id='error-handling') How do you setup an error handler in Express?
p.
View
@@ -36,7 +36,17 @@
to remain on that branch. For migration information visit the
<a href="https://github.com/visionmedia/express/wiki/Migrating-from-2.x-to-3.x">migration</a>
wiki page, or view a <a href="https://github.com/visionmedia/express/wiki/New-features-in-3.x">list of changes</a> made in 3.x.
-</p></setup><setup><h3 id="error-handling">How do you setup an error handler in Express?</h3><p>Error-handling middleware are defined just like regular middleware,
+</p></setup><setup><h3 id="404-handling">How do you handle 404s?</h3><p>In Express 404s are not the result of an error, thus the
+error-handler middleware will not capture 404s, this is
+because a 404 is simply the absence of additional work to do,
+in other words Express has executed all middleware / routes
+and found that none of them responded. All you need to
+do is add a middleware at the very bottom below all the others
+to handle a 404:
+</p><pre class="js"><code>app.use(function(req, res, next){
+ res.send(404, 'Sorry cant find that!');
+});
+</code></pre></setup><setup><h3 id="error-handling">How do you setup an error handler in Express?</h3><p>Error-handling middleware are defined just like regular middleware,
however must be define with an arity of 4, that is the signature
<code>(err, req, res, next)</code>:
</p><pre class="js"><code>app.use(function(err, req, res, next){

0 comments on commit a4ef925

Please sign in to comment.