### Install finalhandler Source: https://github.com/pillarjs/finalhandler/blob/master/README.md Install the finalhandler module using npm. ```sh $ npm install finalhandler ``` -------------------------------- ### Always return 404 Source: https://github.com/pillarjs/finalhandler/blob/master/README.md Example demonstrating how to use finalhandler to always return a 404 response when no other logic handles the request. ```javascript const finalhandler = require('finalhandler') const http = require('http') const server = http.createServer((req, res) => { const done = finalhandler(req, res) done() }) server.listen(3000) ``` -------------------------------- ### Perform simple action Source: https://github.com/pillarjs/finalhandler/blob/master/README.md Example showing how to use finalhandler with asynchronous file reading to serve an HTML file. If an error occurs during file reading, finalhandler will process the error. ```javascript const finalhandler = require('finalhandler') const fs = require('fs') const http = require('http') const server = http.createServer((req, res) => { const done = finalhandler(req, res) fs.readFile('index.html', (err, buf) => { if (err) return done(err) res.setHeader('Content-Type', 'text/html') res.end(buf) }) }) server.listen(3000) ``` -------------------------------- ### Use with middleware-style functions Source: https://github.com/pillarjs/finalhandler/blob/master/README.md Example integrating finalhandler with middleware-style functions like 'serve-static'. The 'serve' middleware is called, and if it doesn't handle the request, finalhandler takes over. ```javascript const finalhandler = require('finalhandler') const http = require('http') const serveStatic = require('serve-static') const serve = serveStatic('public') const server = http.createServer((req, res) => { const done = finalhandler(req, res) serve(req, res, done) }) server.listen(3000) ``` -------------------------------- ### Keep log of all errors Source: https://github.com/pillarjs/finalhandler/blob/master/README.md Example demonstrating how to provide a custom error logging function to finalhandler using the 'onerror' option. This function will be called with the error object when an error occurs. ```javascript const finalhandler = require('finalhandler') const fs = require('fs') const http = require('http') const server = http.createServer((req, res) => { const done = finalhandler(req, res, { onerror: logerror }) fs.readFile('index.html', (err, buf) => { if (err) return done(err) res.setHeader('Content-Type', 'text/html') res.end(buf) }) }) server.listen(3000) function logerror (err) { console.error(err.stack || err.toString()) } ``` -------------------------------- ### Import finalhandler Source: https://github.com/pillarjs/finalhandler/blob/master/README.md Import the finalhandler module in your Node.js application. ```javascript const finalhandler = require('finalhandler') ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.