← Back to Examples

Node.js Basics

This page shows Node.js concepts as reference code. To run these examples, save them as .js files and run with node filename.js.

Note: Node.js code runs on the server, not in the browser. These code snippets are displayed for reference and learning.

1. Hello World

// hello.js console.log('Hello from Node.js!'); console.log('Node version:', process.version); console.log('Platform:', process.platform); console.log('Architecture:', process.arch);
$ node hello.js
Hello from Node.js!
Node version: v20.11.0
Platform: darwin
Architecture: arm64

2. The Process Object

// process-demo.js // Current working directory console.log('CWD:', process.cwd()); // Command line arguments // Run: node process-demo.js hello world console.log('Arguments:', process.argv); // ['node-path', 'script-path', 'hello', 'world'] // Get user arguments only const args = process.argv.slice(2); console.log('User args:', args); // Environment variables console.log('HOME:', process.env.HOME); console.log('PATH:', process.env.PATH); // Set environment variable process.env.MY_VAR = 'hello'; console.log('MY_VAR:', process.env.MY_VAR); // Memory usage const memory = process.memoryUsage(); console.log('Memory:', { rss: `${(memory.rss / 1024 / 1024).toFixed(2)} MB`, heapUsed: `${(memory.heapUsed / 1024 / 1024).toFixed(2)} MB` }); // Process ID console.log('PID:', process.pid); // Exit with code // process.exit(0); // 0 = success, 1 = error

3. Timers (Same as Browser)

// timers.js // setTimeout - run once after delay setTimeout(() => { console.log('This runs after 2 seconds'); }, 2000); // setInterval - run repeatedly let count = 0; const interval = setInterval(() => { count++; console.log(`Count: ${count}`); if (count >= 5) { clearInterval(interval); console.log('Interval cleared!'); } }, 1000); // setImmediate - run in next iteration (Node.js only) setImmediate(() => { console.log('This runs in the next event loop iteration'); }); // process.nextTick - run before next I/O (Node.js only) process.nextTick(() => { console.log('This runs before any I/O event'); });

4. Event Emitter

// events.js const EventEmitter = require('events'); // Create an emitter const emitter = new EventEmitter(); // Register event listener emitter.on('greet', (name) => { console.log(`Hello, ${name}!`); }); // Register one-time listener emitter.once('welcome', () => { console.log('Welcome! (This only fires once)'); }); // Emit events emitter.emit('greet', 'Alice'); // "Hello, Alice!" emitter.emit('greet', 'Bob'); // "Hello, Bob!" emitter.emit('welcome'); // "Welcome! ..." emitter.emit('welcome'); // (nothing - once already fired) // Custom event-based class class UserService extends EventEmitter { createUser(name) { // ... create user logic ... const user = { id: Date.now(), name }; this.emit('userCreated', user); return user; } } const service = new UserService(); service.on('userCreated', (user) => { console.log('New user created:', user); }); service.createUser('Charlie');

5. The Global Object

// globals.js // In Browser: window, document // In Node.js: global, process // Some globals available in Node.js: console.log(typeof global); // 'object' console.log(typeof process); // 'object' console.log(typeof __dirname); // 'string' (current directory) console.log(typeof __filename); // 'string' (current file) console.log(typeof require); // 'function' console.log(typeof module); // 'object' console.log(typeof exports); // 'object' // __dirname and __filename console.log('Directory:', __dirname); // /Users/you/projects/my-app console.log('File:', __filename); // /Users/you/projects/my-app/globals.js // Note: __dirname and __filename are NOT available // in ES Modules. Use import.meta.url instead: // import { fileURLToPath } from 'url'; // const __filename = fileURLToPath(import.meta.url);

6. Buffer (Binary Data)

// buffer.js // Create buffer from string const buf1 = Buffer.from('Hello Node.js'); console.log(buf1); // <Buffer 48 65 6c 6c 6f ...> console.log(buf1.toString()); // 'Hello Node.js' console.log(buf1.length); // 13 (bytes) // Create empty buffer const buf2 = Buffer.alloc(10); // 10 bytes, filled with 0 console.log(buf2); // <Buffer 00 00 00 00 ...> // Write to buffer buf2.write('Hi'); console.log(buf2.toString()); // 'Hi' // Buffer encoding const buf3 = Buffer.from('Hello', 'utf-8'); console.log(buf3.toString('hex')); // '48656c6c6f' console.log(buf3.toString('base64')); // 'SGVsbG8=' // Useful for: file operations, network data, cryptography

← Back to Examples