DEV Community

SOVANNARO
SOVANNARO

Posted on

Creating Your First Node.js Application πŸš€

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

  1. Go to nodejs.org.
  2. Download the LTS (Long-Term Support) version (it's more stable!).
  3. 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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

This creates a new folder and moves inside it.

πŸ”Ή Initialize Your Project

Inside the folder, run:

npm init -y
Enter fullscreen mode Exit fullscreen mode

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.)
Enter fullscreen mode Exit fullscreen mode

Open app.js and paste this:

console.log("Hello, Node.js!");
Enter fullscreen mode Exit fullscreen mode

Save the file. Now, run the script:

node app.js
Enter fullscreen mode Exit fullscreen mode

Boom! You just executed your first Node.js program, and you should see:

Hello, Node.js!
Enter fullscreen mode Exit fullscreen mode

🌍 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");
});
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή Run Your Server

Now, start your server by running:

node app.js
Enter fullscreen mode Exit fullscreen mode

You should see:

Server is running on http://localhost:3000
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή 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");
});
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή Run the App Again

node app.js
Enter fullscreen mode Exit fullscreen mode

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)