Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var http = require('http');
- var fs = require('fs');
- var path = require('path');
- var url = require('url');
- var port = process.argv[2] || 8888;
- var dirs = {};
- dirs = [".//png//",".//jpg//",".//html//",".//js//"];
- exts = ["png","jpg", "html","js"];
- var myServer = http.createServer(function(req, res) {
- console.log('request starting:'+req.method+" "+req.url);
- //var dirName = path.dirname(req.url).toLowerCase();
- //var baseName = path.basename(req.url).toLowerCase();
- //var extName = path.extname(req.url).toLowerCase();
- // handling plain files .html/.js
- if (req.method.toLowerCase() === 'get') {
- var files = []
- var dir = 0;
- for (i = 0; i < exts.length; i++)
- {
- if (req.url.toLowerCase() === '/' + exts[i])
- {
- path.exists(exts[i], function (exists) {
- if (!exists || fs.statSync(exts[i]).isFile()) {
- console.log('dir for request ' + req.url.toLowerCase() + " not found");
- res.writeHead(404);
- res.end();
- return;
- }
- files = fs.readdirSync(exts[i]);
- console.log(files.length + ' files for request ' + req.url.toLowerCase() + " read");
- dir = i;
- });
- }
- }
- console.log(files.length + ' files for request ' + req.url.toLowerCase() + " read");
- for (i = 0; i < files.length; i++)
- {
- console.log(path.join(dirs[dir], files[i]) + ' file');
- fs.readFile(path.join(dirs[dir], files[i]), function (error, content) {
- console.log(path.join(dirs[dir], files[i]) + ' file');
- if (error) {
- res.writeHead(500);
- res.end();
- console.log("error");
- }
- else {
- res.writeHead(200, { 'Content-Type': 'text/html' });
- //res.write(content, 'utf-8');
- res.end(content, 'utf-8');
- console.log("success");
- }
- });
- }
- return;
- }
- // handling "POST /ocena/1..5" requests
- //if (req.method.toLowerCase() === 'post' && dirName === '/ocena') {
- // sum = sum + parseInt(baseName);
- // count++;
- // currentAverage = sum/count;
- //}
- //if (!!currentAverage) {
- // res.writeHead(200, { 'Content-Type': 'text/html' } );
- // res.end(""+currentAverage,'utf-8');
- // return;
- //}
- res.writeHead(404);
- res.end();
- })
- myServer.listen(parseInt(port, 10));
- 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