⚙️ Backend

Node.js Fundamentals

Last updated: 2025-09-25 02:29:54

Node.js Server-Side JavaScript

Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine.

Getting Started

// Hello World Server
const http = require('http');

const server = http.createServer((req, res) => {
  res.writeHead(200, { 'Content-Type': 'text/html' });
  res.end('

Hello World!

'); }); server.listen(3000, () => { console.log('Server running on port 3000'); });

Express.js Framework

const express = require('express');
const app = express();

// Middleware
app.use(express.json());
app.use(express.static('public'));

// Routes
app.get('/', (req, res) => {
  res.send('Hello Express!');
});

app.post('/api/users', (req, res) => {
  const user = req.body;
  // Process user data
  res.json({ success: true, user });
});

app.listen(3000);

File System Operations

const fs = require('fs').promises;

// Read file
async function readFile(filename) {
  try {
    const data = await fs.readFile(filename, 'utf8');
    return data;
  } catch (error) {
    console.error('Error reading file:', error);
  }
}

// Write file
async function writeFile(filename, data) {
  try {
    await fs.writeFile(filename, data);
    console.log('File written successfully');
  } catch (error) {
    console.error('Error writing file:', error);
  }
}