Skip to main content

Getting Started with Node.js

Node.js is a powerful JavaScript runtime built on Chrome's V8 engine. It lets you run JavaScript outside the browser and is widely used for building fast, scalable backend applications.


Why Learn Node.js?

  • JavaScript everywhere – use the same language for frontend & backend.
  • Fast & scalable – event-driven, non-blocking I/O.
  • Huge ecosystem – over a million npm packages.
  • Community support – large developer community and open-source projects.

Setting Up Node.js

  1. Download and install from Node.js official site.

  2. Verify installation:

    node -v
    npm -v
  3. Create your first file app.js:

    console.log("Hello, Node.js!");
  4. Run it:

    node app.js

npm Basics

npm (Node Package Manager) comes with Node.js. Example: Install Express (popular web framework):


npm init -y
npm install express

Your First Server

Create a simple server with Express:


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

app.get("/", (req, res) => {
res.send("Hello from Node.js 🚀");
});

app.listen(3000, () => {
console.log("Server running at http://localhost:3000");
});

Run:


node app.js

Connecting to a Database

Install MongoDB driver (or Mongoose):

npm install mongoose

Example connection:


const mongoose = require("mongoose");

mongoose.connect("mongodb://localhost:27017/mydb")
.then(() => console.log("✅ Database connected"))
.catch(err => console.error(err));

Next Steps

  • Learn Asynchronous JavaScript (callbacks, promises, async/await).
  • Explore popular frameworks:
  • Express.js → REST APIs
  • Nest.js → enterprise apps
  • Socket.io → real-time apps

Build projects

  • Todo API
  • Chat application
  • Authentication system

Quiz Time

1. What is Node.js primarily used for?

2. Which command checks your installed Node.js version?

3. What file is essential for managing dependencies in a Node.js project?

4. Which built-in Node.js module lets you create a basic server?

5. Which command initializes a new Node.js project with a package.json file?

Conclusion

Node.js gives you the power to use JavaScript everywhere, making development seamless. Whether you're building APIs, real-time apps, or full-stack solutions, Node.js is a must-have in your toolkit.