Learning Node.js? Here’s How I Got My First API Running
When I first started learning Node.js, the thought of building a working API felt intimidating. But with a little focus and the right guidance, I had my first API up and running in just a few hours. In this post, I’ll walk you through exactly how I did it — step by step — so you can do the same, even if you’re just getting started.
Step 1: Set Up Your Environment
I made sure Node.js and npm were installed on my system. You can download both from nodejs.org.
Then, I created a new project folder and initialized it with:
npm init -y
This created a package.json
file to manage my project dependencies.
Step 2: Install Express
I used Express, a popular web framework for Node.js. I installed it with:
npm install express
Express helps you quickly set up routes and handle requests/responses in a clean way.
Step 3: Write the First API Endpoint
I created a file named index.js
and added this simple code:
const express = require('express');
const app = express();
const PORT = 3000;
app.get('/', (req, res) => {
res.send('Hello from my first API!');
});
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
This sets up a basic API that responds with a message when accessed at the root URL.
Step 4: Test It Out
I ran the server with:
node index.js
Then I opened http://localhost:3000 in my browser and saw:
Hello from my first API!
Bonus Tip: Use Postman or Thunder Client
For better API testing, I used tools like Postman or the Thunder Client VS Code extension to send requests and inspect responses.
What’s Next?
- Add more routes (e.g.,
/about
,/users
) - Accept POST requests and parse JSON
- Connect to a database like MongoDB or PostgreSQL
- Deploy your API to platforms like Vercel, Render, or Railway
Final Thoughts
Getting started with Node.js doesn’t have to be hard. Take it one step at a time. If I can get an API running in a day, so can you!
If you found this helpful, feel free to share it or comment with your questions. Happy coding!