Study online at https://quizlet.com/_gafo5j
1. Port Coding to Argument: const port = process.argv[2] || 3032;
Add after initial requirements at top/line 1/2/3
2. CREATE A FOLDER, CHECK IF EXIST: const agentDir = './Agent';
//read and check
fs.readdir(agentDir, (err, content) => {
if (err) {
fs.mkdir(agentDir, (err) => {
if (err) throw err;
console.log('Agent directory created');
});
}
});
3. OBJECT DESTRUCTURING: const { pathname, searchParams } = new URL(req.url, 'http://' + req.head-
ers.host + '/');
4. USER AGENT TRACKING: const userAgent = req.headers['user-agent'];
fs.readFile('./Agent/userAgent.txt', {encoding:'utf-8'}, (err, content)=>{
//check if exist
If (err) {
fs.writeFile('new.txt', userAgent, (err)=>{
If (err) throw err;
console.log('File is created');
});
}else{ //if it exists
fs.appendFile('./Agent/userAgent.txt', userAgent + '\n', (err) => {
If (err) throw err;
console.log('error appending');
});
}
});
1/3