,

Beginner’s Tutorial: Developing a REST API using Node.js and Express.js

Posted by





Building A Rest API with NodeJS and ExpressJS | Beginners Tutorial

Building A Rest API with NodeJS and ExpressJS | Beginners Tutorial

NodeJS and ExpressJS are two popular technologies that are widely used for building web applications. In this tutorial, we will explore how to create a Rest API using these two frameworks.

Prerequisites

  • Basic knowledge of JavaScript
  • NodeJS and NPM installed on your machine

Step 1: Setting up the Project

First, we need to set up a new NodeJS project. Open your terminal or command prompt and run the following command:

mkdir rest-api
cd rest-api
npm init

Follow the prompts and fill in the necessary details for your project.

Step 2: Installing Dependencies

We will be using ExpressJS, a minimalistic web application framework for NodeJS. To install ExpressJS, run the following command:

npm install express

Step 3: Creating the API

Create a new file called index.js and open it in your code editor. This will be the entry point of our application.

const express = require('express');
const app = express();
const port = 3000;

app.get('/', (req, res) => {
  res.send('Hello World!');
});

app.listen(port, () => {
  console.log(`Server running on port ${port}`);
});

Step 4: Starting the Server

To start the server, simply run the following command in your terminal:

node index.js

You should see a message indicating that the server is running on port 3000.

Step 5: Testing the API

Open your browser and navigate to http://localhost:3000. You should see the message “Hello World!” displayed in your browser, indicating that the API is working correctly.

Step 6: Adding Routes

Now let’s add some routes to our API. Modify the index.js file as follows:

app.get('/api', (req, res) => {
  res.json({
    message: 'Welcome to our API!'
  });
});

app.post('/api/users', (req, res) => {
  // Handle user creation logic here
});

app.put('/api/users/:id', (req, res) => {
  // Handle user update logic here
});

app.delete('/api/users/:id', (req, res) => {
  // Handle user deletion logic here
});

Step 7: Testing the Routes

You can now test the routes using a tool like Postman or by sending HTTP requests using JavaScript. For example, to test the GET route, send a GET request to http://localhost:3000/api. You should receive a JSON response with the message “Welcome to our API!”.

Conclusion

Congratulations! You have successfully built a basic Rest API using NodeJS and ExpressJS. This tutorial covered the essential steps to set up a project, install dependencies, create routes, and test the API. From here, you can continue to expand and enhance your API based on your specific requirements.


0 0 votes
Article Rating
20 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Ramgopal Sitamraju
8 months ago

Thank You Mr. Pedro Nice One !

K.
K.
8 months ago

Thanks for the video Pedro!!!

Ethan Hunt
8 months ago

Well this is dope!

Nna Benjamin
8 months ago

I love your tutorials

Touché
8 months ago

Great tutorial! Really appreciate it🙏🏾Just subscribed and gonna check out more of your videos!😁

Richard Ogujawa - Old Account
8 months ago

Quick Tips: 
Dunno if someone already mentioned these but…

1) You can bypass having to press Enter for all the prompts/questions you're asked when you do "npm init", if you do "npm init -y" instead. The -y flag means "say yes to everything."
2) If the server your running is set up in the default file or the entry point (to check what the entry point is just look at the value assigned to the key "main" in your package.json file) you could just do "node ." . The dot at the end just means to run default file in the current directory. 

Great video by the way Pedro, learned a lot!

Mario Larios
8 months ago

I can't believe I haven't ran into your videos, they are the best explanations out there.

trijay madhu
8 months ago

Great Tutorial

Salim Ghanem
8 months ago

@8:12 what is that homepage??

Gloria Jeptepkeny
8 months ago

Thank you so much for the video ..the explanation is very clear

Thang Phan
8 months ago

Thank you so much, really appreciate it 🙂

YASEENI MUHAMMADRAJA
8 months ago

Great explanation👍❤️👊.
Can you give Source code pls?

TheNamesJT
8 months ago

33:00 couldn't you of used the npm package uuid then added some logic to the post request and put request? like const newUser = req.body then do if newUser._id === " " then you would just do newUser._id = uuidv4() which would auto generate you a id if the key: value is empty. You could of also added more logic for if the !newUser._id meaning if there isn't a _id in the request then you could throw an error saying add the _id key with an empy value string to have it auto generate.

Abdul Ahad
8 months ago

I've been watching videos the whole day trying to understand the basics of express and connecting to database , and this one is truly the best one out there .

Cristián De Gracia Nuero
8 months ago

This channel is awesome.

Rakibul Hasan
8 months ago

Trust me one of the BEST YouTuber, I am not getting why arent you in the spotlight? There isn't much resource about backend in youtube and you got them. Congrats

Julian Sosa
8 months ago

Thanks, a really good video tutorial!

Colin Pereira
8 months ago

This is an amazing video! Thank you

Tyz96ion
8 months ago

great tutorial man! Love you <3

Anaeze Somtochukwu
8 months ago

Can I use phpmyadmin MySQLdatabase ?