×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
1
Language: Javascript
Posted by: Isaac Dettman
Added: Apr 28, 2017 3:56 PM
Views: 11
  1. const http = require('http')
  2. const url = require('url')
  3. const fs = require('fs')
  4.  
  5. http.createServer((req, response) => {
  6.        
  7.         try {
  8.                 const requestUrl = url.parse(req.url)
  9.                 const filename = (requestUrl.pathname.indexOf('.') === -1) ? 'index.html' : requestUrl.pathname.substring(1)
  10.                 response.writeHead(200)
  11.                 fs.createReadStream(filename).pipe(response)
  12.         }
  13.         catch (e) {
  14.                 response.writeHead(500)
  15.                 response.end()
  16.                 console.log(e.stack)
  17.         }
  18. }).listen(3000)
  19.  
  20.