Advertisement
desdemona

nodejs

Dec 18th, 2014
613
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.  
  39.         for (i = 0; i < files.length; i++)
  40.         {
  41.             fs.readFile(path.join(dirs[dir], files[i]), function (error, content) {
  42.                 console.log(path.join(dirs[dir], files[i]) + ' file');
  43.                 if (error) {
  44.                     res.writeHead(500);                  
  45.                     res.end();
  46.                     console.log("error");
  47.                 }
  48.                 else {
  49.                     res.writeHead(200, { 'Content-Type': 'text/html' });
  50.                     //res.write(content, 'utf-8');
  51.                     res.end(content, 'utf-8');
  52.                     console.log("success");
  53.                 }
  54.             });
  55.         }  
  56.  
  57.         return;
  58.     }
  59.    
  60.     // handling "POST /ocena/1..5" requests
  61.     //if (req.method.toLowerCase() === 'post' && dirName === '/ocena') {
  62.     //  sum = sum + parseInt(baseName);
  63.     //  count++;
  64.     //  currentAverage = sum/count;
  65.     //}
  66.  
  67.     //if (!!currentAverage) {
  68.     //  res.writeHead(200, { 'Content-Type': 'text/html' } );
  69.     //  res.end(""+currentAverage,'utf-8');
  70.     //  return;
  71.     //}
  72.  
  73.     res.writeHead(404);
  74.     res.end();
  75. })
  76. myServer.listen(parseInt(port, 10));
  77. 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