Skip to main content

Posts

Showing posts with the label Node.js Basics

How to Set Up and Run Your First Node.js Project

How to Set Up and Run Your First Node.js Project New to Node.js? This guide walks you through setting up your first Node.js project from scratch—whether you’re building APIs, CLI tools, or learning backend development. 1. What is Node.js? Node.js is a runtime environment that allows you to run JavaScript on the server side. It’s fast, event-driven, and perfect for building scalable web apps and services. 2. Install Node.js Download and install Node.js from the official website: https://nodejs.org Once installed, verify it in your terminal: node -v npm -v 3. Create Your Project Folder Open your terminal and create a new directory for your project: mkdir my-first-node cd my-first-node 4. Initialize a New Node.js Project Use npm (Node Package Manager) to create a package.json file: npm init -y This will generate a basic configuration file to manage your project dependencies. 5. Create Your First Script ...

Node.js Path Module – Complete Notes with Real Use Cases

Node.js Path Module – Notes with Use Cases 💡 The path module in Node.js provides utilities to work with file and directory paths. It is built-in and can be imported using: const path = require('path'); ✅ path.basename(path) Returns: The filename from a full path. Use Case: Extract file name during file upload logging. path.basename('C:\\temp\\abcd.html'); // Output: 'abcd.html' ✅ path.dirname(path) Returns: The directory portion of the path. Use Case: Get parent directory before saving a file. path.dirname('foo\\abcd\\test.html'); // Output: 'foo\\abcd' ✅ path.extname(path) Returns: The file extension from the path. Use Case: Validate file type before processing upload. path.extname('test.html'); // '.html' path.extname('file.name.md'); // '.md' path.extname('.hiddenfile.txt'); // '.txt' path.extnam...