[Pkg-javascript-commits] [node-static] 24/151: (doc) document serveFile

Tonnerre Lombard tonnerre-guest at moszumanska.debian.org
Tue Jan 7 23:17:56 UTC 2014


This is an automated email from the git hooks/post-receive script.

tonnerre-guest pushed a commit to branch master
in repository node-static.

commit 76896a572d7e9895bbce1b4fe70ff12e2e181b3a
Author: cloudhead <self at cloudhead.net>
Date:   Fri Aug 6 00:45:58 2010 -0400

    (doc) document serveFile
---
 README.md | 25 +++++++++++++++++++++++--
 1 file changed, 23 insertions(+), 2 deletions(-)

diff --git a/README.md b/README.md
index f756593..1df6f2b 100644
--- a/README.md
+++ b/README.md
@@ -48,9 +48,9 @@ You can also specify how long the client is supposed to cache the files node-sta
 This will set the `Cache-Control` header, telling clients to cache the file for an hour.
 This is the default setting.
 
-### Serving files #
+### Serving files under a directory #
 
-To serve files, simply call the `serve` method on a `Server` instance, passing it
+To serve files under a directory, simply call the `serve` method on a `Server` instance, passing it
 the HTTP request and response object:
 
     var fileServer = new static.Server('./public');
@@ -61,6 +61,27 @@ the HTTP request and response object:
         });
     }).listen(8080);
 
+### Serving specific files #
+
+If you want to serve a specific file, like an error page for example, use the `serveFile` method:
+
+    fileServer.serveFile('/error.html', request, response);
+
+This will serve the `error.html` file, from under the file root directory. For example, you could
+serve an error page, when the initial request wasn't found:
+
+    require('http').createServer(function (request, response) {
+        request.addListener('end', function () {
+            fileServer.serve(request, response, function (e, res) {
+                if (e && (e.status === 404)) { // If the file wasn't found
+                    fileServer.serveFile('/not-found.html', request, response);
+                }
+            });
+        });
+    }).listen(8080);
+
+More on intercepting errors bellow.
+
 ### Intercepting errors & Listening #
 
 An optional callback can be passed as last argument, it will be called every time a file

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-static.git



More information about the Pkg-javascript-commits mailing list