Skip to content

Latest commit

 

History

History
58 lines (50 loc) · 1.57 KB

section34.1.md

File metadata and controls

58 lines (50 loc) · 1.57 KB

Section 34.1: Framework-less node server

let http = require('http');
let fs = require('fs');
let path = require('path');

http.createServer(function (request, response) {
  console.log('request ', request.url);
  let filePath = '.' + request.url;

  if (filePath == './') filePath = './index.html';

  let extname = String(path.extname(filePath)).toLowerCase();
  let contentType = 'text/html';

  let mimeTypes = {
    '.html': 'text/html',
    '.js': 'text/javascript',
    '.css': 'text/css',
    '.json': 'application/json',
    '.png': 'image/png',
    '.jpg': 'image/jpg',
    '.gif': 'image/gif',
    '.wav': 'audio/wav',
    '.mp4': 'video/mp4',
    '.woff': 'application/font-woff',
    '.ttf': 'applilcation/font-ttf',
    '.eot': 'application/vnd.ms-fontobject',
    '.otf': 'application/font-otf',
    '.svg': 'application/image/svg+xml'
  };

  contentType = mimeTypes[extname] || 'application/octect-stream';

  fs.readFile(filePath, function(error, content) {
    if (error) {
      if(error.code == 'ENOENT'){
        fs.readFile('./404.html', function(error, content) {
          response.writeHead(200, { 'Content-Type': contentType });
          response.end(content, 'utf-8');
        });
      }
      else {
        response.writeHead(500);
        response.end('Sorry, check with the site admin for error: '+error.code+' ..\n');
        response.end();
      }
    }
    else {
      response.writeHead(200, { 'Content-Type': contentType });
      response.end(content, 'utf-8');
    }
  });
}).listen(8125);

console.log('Server running at http://127.0.0.1:8125/');