๐Ÿ Node.js Introduction & Getting Started
Estimated reading: 3 minutes 38 views

๐Ÿ› ๏ธ Get Started with Node.js โ€“ Build Your First Application and HTTP Server


๐Ÿงฒ Introduction โ€“ Your First Step into Node.js Development

Building your first application in Node.js is quick, simple, and highly rewarding. This guide walks you through setting up your Node.js environment, writing your first script, and understanding the building blocks of a Node.js application.

๐ŸŽฏ In this guide, youโ€™ll learn:

  • How to set up Node.js on your machine
  • How to write and run your first Node.js script
  • How to create a basic HTTP server
  • Best practices to follow in your first Node project

โš™๏ธ Step 1: Install Node.js

Visit https://nodejs.org and download the LTS version for your operating system.

After installation, verify with:

node -v     # Check Node.js version
npm -v      # Check npm version

You should see output like:

v20.0.0
10.0.0

๐Ÿ“ Step 2: Write Your First Node.js Script

Create a file named app.js and add this code:

// app.js - Your first Node.js script
console.log('Hello, Node.js!');

Run the file using the command:

node app.js

๐Ÿงช Output:

Hello, Node.js!

๐ŸŒ Step 3: Build a Simple HTTP Server

Now let’s build a simple web server using Node.js:

// server.js - Basic HTTP server
const http = require('http');

// Create server object
const server = http.createServer((req, res) => {
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.end('Welcome to your first Node.js server!');
});

// Listen on port 3000
server.listen(3000, () => {
  console.log('Server running at http://localhost:3000/');
});

Run this with:

node server.js

Then open your browser and visit http://localhost:3000.

๐Ÿงช Output:

Welcome to your first Node.js server!

๐Ÿ“‚ Step 4: Create a Node Project with NPM

To manage dependencies, initialize your project with:

npm init -y

This creates a package.json file:

{
  "name": "my-first-node-app",
  "version": "1.0.0",
  "main": "app.js",
  "license": "MIT"
}

๐Ÿงฐ Best Practices for First Application

โœ… Practice๐Ÿ’ก Recommendation
Use const and letAvoid var to prevent scope-related bugs
Modularize your codeUse separate files and modules (require)
Ignore node_modulesAlways use .gitignore for version control
Log clearlyUse console.log effectively for debugging
Handle errors gracefullyUse try/catch and validate inputs

๐Ÿ“Œ Summary โ€“ Recap & Next Steps

Youโ€™ve just built and run your first Node.js application! This sets the stage for building real-time apps, REST APIs, and more advanced server-side logic.

๐Ÿ” Key Takeaways:

  • Install Node.js and run .js files using the terminal
  • Build a simple server using the http module
  • Initialize your project using npm init
  • Organize and structure your app using modules

โš™๏ธ Real-world relevance:
These fundamentals are used in microservices, web APIs, and full-stack JavaScript projects.


โ“FAQs โ€“ First Application in Node.js


โ“ How do I run a Node.js file?
โœ… Use the terminal command:

node filename.js

โ“ Do I need a server like Apache with Node.js?
โœ… No. Node.js has its own built-in server module (http) and does not need an external web server.


โ“ Can I use Node.js with HTML pages?
โœ… Yes. You can serve HTML files using the fs module or use frameworks like Express.js to render pages.


โ“ What is the default port for Node.js servers?
โœ… There is no default. You can specify any open port using server.listen(PORT).


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

๐Ÿ› ๏ธ Node.js โ€“ Get Started / First Application

Or Copy Link

CONTENTS
Scroll to Top