Hey there, awesome devs! π Ready to build something cool with Node.js? If youβve never touched Node.js before, donβt worry! Today, Iβm going to walk you through creating your first Node.js applicationβstep by step, in a fun and easy way. π
By the end of this blog, you'll have your very own Node.js app running, and trust me, it's going to feel amazing!
π Step 1: Install Node.js
First things first, you need to install Node.js on your machine.
πΉ Download and Install
- Go to nodejs.org.
- Download the LTS (Long-Term Support) version (it's more stable!).
- Follow the installation process like any other software.
πΉ Check If Node.js is Installed
Once installed, open your terminal (Command Prompt, PowerShell, or macOS Terminal) and run:
node -v
If you see a version number like v22.x.x
, congrats! π Node.js is installed!
π Step 2: Create a Simple Node.js App
Now, letβs write some real Node.js code! π
πΉ Create a New Project Folder
Choose a place on your computer and run:
mkdir my-first-node-app
cd my-first-node-app
This creates a new folder and moves inside it.
πΉ Initialize Your Project
Inside the folder, run:
npm init -y
This creates a package.json
file, which keeps track of your project details. Donβt worry about it for now!
πΉ Create Your First Node.js File
Now, letβs create our first Node.js script. Run:
touch app.js # (or use Notepad, VS Code, etc.)
Open app.js
and paste this:
console.log("Hello, Node.js!");
Save the file. Now, run the script:
node app.js
Boom! You just executed your first Node.js program, and you should see:
Hello, Node.js!
π Step 3: Create a Simple Web Server
Now, letβs build something more usefulβyour own web server! π
πΉ Modify app.js
Replace the content of app.js
with this:
const http = require("http");
const server = http.createServer((req, res) => {
res.writeHead(200, { "Content-Type": "text/plain" });
res.end("Hello, World from Node.js! π");
});
server.listen(3000, () => {
console.log("Server is running on http://localhost:3000");
});
πΉ Run Your Server
Now, start your server by running:
node app.js
You should see:
Server is running on http://localhost:3000
Open your browser and go to http://localhost:3000. π Boom! You just created a web server!
π Step 4: Make It Even Cooler
Want to take it up a notch? Letβs use Express.js, a popular framework for building web apps in Node.js.
πΉ Install Express
Run this inside your project folder:
npm install express
πΉ Update app.js
Replace the code in app.js
with this:
const express = require("express");
const app = express();
app.get("/", (req, res) => {
res.send("Hello, World from Express! π");
});
app.listen(3000, () => {
console.log("Express server running on http://localhost:3000");
});
πΉ Run the App Again
node app.js
Visit http://localhost:3000 in your browser, and youβll see Hello, World from Express! π.
Congrats! π Youβve just built your first Express.js web app!
π₯ Whatβs Next?
Now that youβve built your first Node.js app, here are some cool things to explore:
β
Learn about npm packages (try npm install chalk
for colorful console logs!).
β
Build a REST API using Express.
β
Connect to a database (MongoDB, PostgreSQL, etc.).
β
Deploy your app online (with Heroku, Vercel, or DigitalOcean).
π Wrap-Up
Node.js is powerful, fast, and fun to learn! Youβve just written your first Node.js application, created a web server, and even played around with Express.js. π
If this blog helped you, make sure to follow me on GitHub π github.com/sovannaro and drop a β. Your support keeps me motivated to create more awesome content! π
Happy coding! π»π₯
Top comments (0)