i'm in bit of predicament. i'm new coding in general, , assigned program javascript class week. supposed create web server serves files file system, implements node modules. must able read html, css, gif, , js resources. these files placed within same folder script, under /scripts , /html directory. here logic have tried implement, terminal keeps telling me path.extname(resource) undefined function. it's quite ugly, know, i'm still trying wrap head around concept. great, have been trying figure out past day.
'use strict'; // load file system module var fs = require('fs'); // load http module var http = require('http'); // load url module var url = require('url'); // load path module var pathmodule = require('path'); // following function called when server // handling request function servepage(request, response) { // path name , remove '/' var path = url.parse(request.url).pathname.substring(1); // if path empty string if (path === '') { path = './html/home.html'; } // read file asynchronously; html fs.readfile(path, function (err, content) { var extension = path.extname(path); switch (extension){ case '.hmtl': response.writehead(200, {'content-type': 'image/gif'}); response.write(content); // send file contents response body response.end(); break; case '.gif': response.writehead(200, {'content-type': 'text/html; charset = utf-8'}); response.write(content); // send file contents response body response.end(); break; case '.css': response.writehead(200, {'content-type': 'text/css; charset = utf-8'}); response.write(content); // send file contents response body response.end(); break; case '.js': response.writehead(200, {'content-type': 'application/javascript; charset = utf-8'}); response.write(content); // send file contents response body response.end(); break; case 'err': response.writehead(404, {'content-type': 'text/plain; charset = utf-8'}); response.write(err.message + ' - page requested not found.'); response.end(); // done break; } /* } if (err) { // if there error, set status code response.writehead(404, {'content-type': 'text/plain; charset = utf-8'}); response.write(err.message + ' - page requested not found.'); response.end(); // done // else if content succesfully read } else { response.writehead(200, {'content-type': 'text/html; charset = utf-8'}); response.write(content); // send file contents response body response.end(); } */ }); } // create server object var server = http.createserver(servepage); server.listen(8080); console.log('server running @ http://localhost:8080');
here how restructure code.
'use strict'; var fs = require('fs'); var http = require('http'); var url = require('url'); var path = require('path'); function getcontenttype(ext) { switch (ext.tolowercase()) { case '.html': return 'text/html; charset=utf-8'; case '.gif': return 'image/gif'; case '.css': return 'text/css; charset=utf-8'; case '.js': return 'application/javascript; charset=utf-8'; default: return 'text/plain; charset=utf-8'; } } var server = http.createserver(function (request, response) { // dangerous. var filepath = url.parse(request.url).pathname.substring(1); if (filepath === '') { filepath = './html/home.html'; } fs.readfile(filepath, function (err, content) { var statuscode = 200, headers = { 'content-type': getcontenttype(path.extname(filepath)) }; if (err) { statuscode = 404; headers['content-type'] = getcontenttype('.txt'); content = 'the page requested not found.\n\n' + err.message; } response.writehead(statuscode, headers); response.write(content); response.end(); console.log('' + statuscode + ': ' + filepath); }); }); server.listen(8080); console.log('server running @ http://localhost:8080');
note
var filepath = url.parse(request.url).pathname.substring(1);
is dangerous. think happens when requests /../../foo/bar
.
Comments
Post a Comment