Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Node.js or Java Script Expert : please need help I need you to update server.js file to add support to JPEG ------server.js----- var fs =
Node.js or Java Script Expert : please need help
I need you to update server.js file to add support to JPEG
------server.js----- var fs = require('fs'); var path = require('path'); var http = require('http'); function send404(response) { response.writeHead(404, { 'Content-Type': 'text/html' }); response.write('Error 404: Resource not found.'); response.end(); } var mimeLookup = { '.js': 'application/javascript', '.html': 'text/html', }; var server = http.createServer(function (req, res) { if (req.method == 'GET') { // resolve file path to filesystem path var fileurl; if (req.url == '/') fileurl = '/index.html'; else fileurl = req.url; var filepath = path.resolve('./' + fileurl); // lookup mime type var fileExt = path.extname(filepath); var mimeType = mimeLookup[fileExt]; if (!mimeType) { send404(res); return; } // see if we have that file fs.exists(filepath, function (exists) { // if not if (!exists) { send404(res); return; }; // finally stream the file res.writeHead(200, { 'content-type': mimeType }); fs.createReadStream(filepath).pipe(res); }); } else { send404(res); } }).listen(3000); console.log('server running on port 3000');
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started