0% found this document useful (0 votes)
8 views3 pages

Node.js Beginner Tasks in VS Code

The document outlines ten basic Node.js tasks for beginners, including creating a console greeting, setting up a basic HTTP server, and implementing simple routing. Additional tasks cover event handling, file operations, a timer event, a JSON server, a command-line calculator, serving a local HTML file, and an event logging system. Each task includes code snippets and execution instructions for practical learning in a VS Code environment.

Uploaded by

Firoza
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

Node.js Beginner Tasks in VS Code

The document outlines ten basic Node.js tasks for beginners, including creating a console greeting, setting up a basic HTTP server, and implementing simple routing. Additional tasks cover event handling, file operations, a timer event, a JSON server, a command-line calculator, serving a local HTML file, and an event logging system. Each task includes code snippets and execution instructions for practical learning in a VS Code environment.

Uploaded by

Firoza
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Node.

js Basic Tasks for Beginners (VS Code


Practice)

Task 1: Console Greeting


[Link]('Hello, ' + [Link][2] + '! Welcome to [Link] ■');
Execution & Output:
Run: node [Link] Firoza Output: Hello, Firoza! Welcome to [Link] ■

Task 2: Basic HTTP Server


const http = require('http');
[Link]((req, res) => {
[Link]('Hello from [Link] server!');
}).listen(3000);
[Link]('Server listening on port 3000');
Execution & Output:
Run: node [Link] Visit: [Link] Output: Hello from [Link] server!

Task 3: Simple Routing


const http = require('http');
[Link]((req, res) => {
if ([Link] === '/about') [Link]('About Page');
else if ([Link] === '/contact') [Link]('Contact Us');
else [Link]('Home Page');
}).listen(3000);
[Link]('Server started on 3000');
Execution & Output:
Run: node [Link] Visit /, /about, /contact to see different pages.

Task 4: Custom Event Example


const EventEmitter = require('events');
const emitter = new EventEmitter();
[Link]('login', (user) => {
[Link](`${user} has logged in!`);
});
[Link]('login', 'Firoza');
Execution & Output:
Run: node [Link] Output: Firoza has logged in!

Task 5: File Read and Write


const fs = require('fs');
[Link]('[Link]', 'Hello [Link]');
[Link]([Link]('[Link]', 'utf8'));
Execution & Output:
Run: node [Link] Output: Hello [Link]

Task 6: Timer Event


let count = 0;
const interval = setInterval(() => {
[Link]('Hello Node');
if (++count === 5) clearInterval(interval);
}, 2000);
Execution & Output:
Run: node [Link] Output: prints 'Hello Node' 5 times at 2s intervals.

Task 7: JSON Server


const http = require('http');
const fs = require('fs');
[Link]((req, res) => {
if ([Link] === '/students') {
const data = [Link]([Link]('[Link]'));
[Link](200, {'Content-Type': 'application/json'});
[Link]([Link](data));
} else {
[Link](404);
[Link]('Not found');
}
}).listen(3000);
[Link]('JSON server running on port 3000');
Execution & Output:
Run: node [Link] Visit: [Link] Output: JSON data from [Link]

Task 8: Command-Line Calculator


const a = parseFloat([Link][2]);
const b = parseFloat([Link][3]);
const op = [Link][4];
if (op === 'add') [Link](a + b);
else if (op === 'sub') [Link](a - b);
else if (op === 'mul') [Link](a * b);
else if (op === 'div') [Link](b !== 0 ? a / b : 'Error: divide by zero');
else [Link]('Unknown operation');
Execution & Output:
Run: node [Link] 10 5 mul Output: 50

Task 9: Serve Local HTML File


const http = require('http');
const fs = require('fs');
[Link]((req, res) => {
const html = [Link]('[Link]');
[Link](200, {'Content-Type': 'text/html'});
[Link](html);
}).listen(3000);
[Link]('Static server on 3000');
Execution & Output:
Run: node [Link] Visit: [Link] Output: displays local HTML page

Task 10: Event Logging System


const EventEmitter = require('events');
const fs = require('fs');
const emitter = new EventEmitter();
[Link]('log', (msg) => {
[Link]('[Link]', `${new Date().toISOString()} - ${msg}\n`);
});
[Link]('log', 'Server started');
[Link]('log', 'File created');
[Link]('Events emitted');
Execution & Output:
Run: node [Link] Output: writes timestamped logs in [Link]

You might also like