const fs = require('fs') const http = require('http') const path = require('path') const server = http.createServer((req, res) => { // `req` is the request object; `res` is the response // Use the requested URL to serve a file in the `files` directory const readStream = fs.createReadStream(path.join('files', req.url)) readStream.on('error', _ => { // Tell the client if the file didn't exist res.write('File not found') res.end() }) // `res` is a writable stream. Piping the file to it sends it to the client. readStream.pipe(res) }) // Listen for requests on port 80 (the default for HTTP) server.listen(80)