Advertisement
desdemona

;(

Dec 18th, 2014
570
0
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. var url = require('url');
  5.  
  6. var port = process.argv[2] || 8888;
  7. var dirs = {};
  8. dirs = [".//png//",".//jpg//",".//html//",".//js//"];
  9. exts = ["png","jpg", "html","js"];
  10.  
  11. var myServer = http.createServer(function(req, res) {
  12.     console.log('request starting:'+req.method+" "+req.url);
  13.     //var dirName = path.dirname(req.url).toLowerCase();
  14.     //var baseName = path.basename(req.url).toLowerCase();
  15.     //var extName = path.extname(req.url).toLowerCase();
  16.        
  17.     // handling plain files .html/.js
  18.     if (req.method.toLowerCase() === 'get') {
  19.         var files = []
  20.         var dir = 0;
  21.         for (i = 0; i < exts.length; i++)
  22.         {
  23.             if (req.url.toLowerCase() === '/' + exts[i])
  24.             {
  25.                 path.exists(exts[i], function (exists) {
  26.                     if (!exists || fs.statSync(exts[i]).isFile()) {
  27.                         console.log('dir for request ' + req.url.toLowerCase() + " not found");
  28.                         res.writeHead(404);
  29.                         res.end();
  30.                         return;
  31.                     }
  32.                     files = fs.readdirSync(exts[i]);
  33.                     console.log(files.length + ' files for request ' + req.url.toLowerCase() + " read");
  34.                     dir = i;
  35.                 });
  36.             }
  37.         }
  38.         console.log(files.length + ' files for request ' + req.url.toLowerCase() + " read");
  39.         for (i = 0; i < files.length; i++)
  40.         {
  41.             console.log(path.join(dirs[dir], files[i]) + ' file');
  42.             fs.readFile(path.join(dirs[dir], files[i]), function (error, content) {
  43.                 console.log(path.join(dirs[dir], files[i]) + ' file');
  44.                 if (error) {
  45.                     res.writeHead(500);                  
  46.                     res.end();
  47.                     console.log("error");
  48.                 }
  49.                 else {
  50.                     res.writeHead(200, { 'Content-Type': 'text/html' });
  51.                     //res.write(content, 'utf-8');
  52.                     res.end(content, 'utf-8');
  53.                     console.log("success");
  54.                 }
  55.             });
  56.         }  
  57.  
  58.         return;
  59.     }
  60.    
  61.     // handling "POST /ocena/1..5" requests
  62.     //if (req.method.toLowerCase() === 'post' && dirName === '/ocena') {
  63.     //  sum = sum + parseInt(baseName);
  64.     //  count++;
  65.     //  currentAverage = sum/count;
  66.     //}
  67.  
  68.     //if (!!currentAverage) {
  69.     //  res.writeHead(200, { 'Content-Type': 'text/html' } );
  70.     //  res.end(""+currentAverage,'utf-8');
  71.     //  return;
  72.     //}
  73.  
  74.     res.writeHead(404);
  75.     res.end();
  76. })
  77. myServer.listen(parseInt(port, 10));
  78. console.log("Static file server running at\n  => http://localhost:" + port + "/\nCTRL + C to shutdown")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement