SHARE
TWEET

Untitled

a guest May 21st, 2019 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var http = require('http');
  2. var fs = require('fs');
  3. var path = require('path');
  4.  
  5. //extensions and such
  6. function fileType(direc){
  7.  
  8.     if(path.extname(direc) == '.js'){
  9.         return 'text/javascript';
  10.     }
  11.     if(path.extname(direc) == '.html'){
  12.         return 'text/html';
  13.     }
  14.     if(path.extname(direc) == '.ico'){
  15.         return 'image/icon';
  16.     }
  17.     if(path.extname(direc) == '.css'){
  18.         return 'text/css';
  19.     }
  20.     else{
  21.         return 'text/html';
  22.     }
  23. }
  24. //server and handeling
  25. var server = http.createServer(function (req, res){
  26.     var direc = './public' + req.url;
  27.     if(direc == './public/'){
  28.         direc = './public/index.html';
  29.     }
  30.    
  31.     fs.readFile(direc, function(err, data) {
  32.        
  33.         if(err){
  34.  
  35.             fs.readFile('./public/404.html', function(err, data){
  36.                 res.writeHead(200, {'Content-Type': fileType(direc), 'Cache-Control': 'max-age = 100'});
  37.                 res.end(data);
  38.             });
  39.         }
  40.         if(!err){
  41.  
  42.             res.writeHead(200, {'Content-Type': fileType(direc), 'Cache-Control': 'max-age = 100'});
  43.             res.end(data);
  44.         }
  45.     });
  46. });
  47.  
  48. server.listen(3000);
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top