This web server supports HTML files and JavaScript files. However it does not su
ID: 3723209 • Letter: T
Question
This web server supports HTML files and JavaScript files. However it does not support any JPEG image files. Your job is to modify this web server so it will support JPEG image files. In the midterm file there is pic1.jpg, pic.jpg, and pic3.jpg. There is also index.html and a server.js file. Modify the server.js file so that it will read and show the three jpg images as a slideshow when you open the html browser page.
Here is the given server.js code below:
var http = require('http');
var fs = require('fs');
var path = require('path');
var mime = require('mime');// calling the npm mime package
function send404(response) {
response.writeHead(404, { 'Content-Type': 'text/plain' });
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');
Explanation / Answer
//mimes means the filetypes, so it searching in the mimeLookup array. So if we ad to list, it will accept
var http = require('http');
var fs = require('fs');
var path = require('path');
var mime = require('mime');// calling the npm mime package
function send404(response) {
response.writeHead(404, { 'Content-Type': 'text/plain' });
response.write('Error 404: Resource not found.');
response.end();
}
var mimeLookup = {
'.js': 'application/javascript',
'.html': 'text/html',
'.jpeg': 'image/jpeg',
'.jpg': 'image/jpeg'
};
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');
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.