Question
Node.js Expert: need help modify app.js file in this project so that your Node.js web server will log all client access requests to a text
Node.js Expert: need help
modify app.js file in this project so that your Node.js web server will log all client access requests to a text file called access.log. The format of the log file is:
------------------------------------------------
datetime | user-agent | browser language | original url requested
-----------------------------------------------------------------
Here are some sample log entries from access.log file:
Mon Jul 18 2016 15:46:24 GMT-0400 (Eastern Daylight Time)|Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36|en-US,en;q=0.8,es;q=0.6|/
Mon Jul 18 2016 15:47:12 GMT-0400 (Eastern Daylight Time)|Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko|en-US,es;q=0.8,zh-CN;q=0.5,en;q=0.3|/images/career.jpg
Mon Jul 18 2016 15:46:47 GMT-0400 (Eastern Daylight Time)|Mozilla/5.0 (Windows NT 6.1; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0|en-US,en;q=0.5|/css/interface.css
The above entries show three access attempts to our web site. The first one is from a Chrome browser which accepts only en-US and en languages. The requested URL is /. The second one is from an IE browser which accepts en-US, es, and zh-CN languages. The requested URL is /images/career.jps. The last one is from a FireFox browser which accepts en-US and en languages. The requested URL is /css/interface.css.
We can write a small program to analyze this log file and show the daily hits of each page of our web site. We can also tally the most popular browsers used to access our web site.
You need to submit only the modified app.js file.
Hint:
You need to require the fs module and create a file write stream to write the log to access.log.
Example: var ws = fs.createWriteStream(access.log, {flags: a}); //append
You can re-use the existing custom middleware called logit or create a new one.
You can get user agent, browser language and requested URL by doing this:
var reqHeader = req.headers;
var userAgent = reqHeader[user-agent];
var browserLanguage = reqHeader[accept-language];
var requestedUrl = req.originalUrl;
Here is the app.js file
---------------------app.js--------------
var connect = require('connect'); var http = require('http'); var serveStatic = require('serve-static'); var util = require('util');
// Create an app var app = connect();
// Create a middleware that serves static web pages using third-party module: static-serve var staticMiddleware = serveStatic('public', {index:['index.html', 'index.htm']});
// Create a custom access log middleware to the console function logit(req, res, next) { util.log(util.format('Request received: %s, %s', req.method, req.url)); next(); }
// Mount all the middlewares app.use(logit).use(staticMiddleware).listen(3333); console.log('static web server started on port 3333');
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