Student Activity
Student Activity (30-min)
Step-by-Step Procedure
Debug a Sample Node.js Application with Errors and Identify Issues (15 min):
Step 1: Create a file
buggyApp.js:javascript코드 복사const http = require('http'); const tasks = [ { id: 1, title: 'Task 1' }, { id: 2, title: 'Task 2' } ]; const server = http.createServer((req, res) => { if (req.url === '/tasks') { res.writeHead(200, { 'Content-Type': 'application/json' }); res.end(tasks); // Bug: Not serialized as JSON } else { res.writeHead(404, { 'Content-Type': 'text/plain' }); res.end('Not Found'); // Bug: Missing newline } }); server.listen(3000, () => { console.log('Server is running at http://localhost:3000'); });Step 2: Run the application:
bash코드 복사node buggyApp.jsStep 3: Debug issues:
Use
console.logto checktasksserialization.Replace
res.end(tasks)with:javascript코드 복사res.end(JSON.stringify(tasks));
Step 4: Add a debugger statement:
javascript코드 복사debugger;Step 5: Run the application in debug mode:
bash코드 복사node inspect buggyApp.jsStep 6: Open Chrome DevTools (
chrome://inspect) and inspect the paused state.
Use a Performance Profiling Tool Like
clinic.js(15 min):Step 1: Install
clinicglobally:bash코드 복사npm install -g clinicStep 2: Run the application with
clinic:bash코드 복사clinic flame -- node buggyApp.jsStep 3: Use Postman or a browser to send multiple requests to
http://localhost:3000/tasks.Step 4: Analyze the flamegraph:
Open the generated
.htmlreport.Identify bottlenecks (e.g., blocking tasks, long synchronous operations).
Step 5: Optimize the application by ensuring all I/O operations are asynchronous.
Last updated