,

Create your own API with NodeJS and ExpressJS

Posted by


Build Your Own API Using NodeJS and ExpressJS

APIs (Application Programming Interfaces) are essential components in modern web development. They allow different software applications to communicate with each other, enabling data exchange and interactivity.

In this article, we will explore how to build your own API using NodeJS and ExpressJS, two popular frameworks in the JavaScript ecosystem.

Prerequisites

Before we start building our API, make sure you have the following installed on your machine:

  • Node.js: You can download and install the latest version from the official Node.js website.
  • npm (Node Package Manager): This comes bundled with Node.js installation.

Setting Up a New Project

Let’s start by setting up a new Node.js project.

  1. Create a new directory for your project:
  2. $ mkdir my-api
  3. Navigate to the project directory:
  4. $ cd my-api
  5. Initialize a new Node.js project:
  6. $ npm init -y

    The -y flag tells npm to use default configurations and skip the interactive setup.

  7. Install Express.js:
  8. $ npm install express

Setting Up the Server

Now that we have our project set up, we will create a simple server using Express.js.

Create a new file called index.js in the root of your project directory, and open it in your favorite code editor.

Add the following code to set up the basic server:

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}`);
});

In this code snippet, we first import the Express.js module and create an instance of the Express application.

We define a route for the root URL (/) using the app.get() method. Inside the callback function, we send the response “Hello World!”.

Finally, we start the server on port 3000 using the app.listen() method.

Running the Server

Open a terminal and navigate to your project directory.

Run the following command to start the server:

$ node index.js

You should see the following output:

Server running on port 3000

Navigate to http://localhost:3000 in your browser, and you should see the “Hello World!” message.

Creating API Endpoints

Now that we have a basic server set up, let’s create some API endpoints.

In the index.js file, add the following code below the existing route:

app.get('/api/users', (req, res) => {
  const users = [
    { id: 1, name: 'John Doe' },
    { id: 2, name: 'Jane Smith' },
    { id: 3, name: 'Bob Johnson' },
  ];

  res.json(users);
});

In this code snippet, we define a new route /api/users that will return a list of dummy users in JSON format.

Restart the server by stopping the Node.js process and running node index.js again.

Now, if you navigate to http://localhost:3000/api/users in your browser, you will see the list of users in JSON format.

Conclusion

In this article, we explored how to build your own API using Node.js and Express.js. We learned how to set up a basic server, create API endpoints, and send JSON responses.

Now that you have a foundation, you can continue building your API by adding more routes and functionality.

Remember to always handle errors, validate data, and secure your API to ensure the best performance and user experience.

Happy coding!

0 0 votes
Article Rating
7 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Simon Grimm
7 months ago

Want to learn more about web development or APIs? That's great!

Check out my new platform https://galaxies.dev/ for practical courses & more tutorials 🚀

Hamdy Youssef
7 months ago

Thank you 💙

Abhishek Wilson
7 months ago

Can I get the documentation link for the tsconfig file please ? I wanted to read on it.

Matheus82
7 months ago

Consegue criar alguma coisa usando PARCELJS

Tony Ross
7 months ago

NPM is installed when you install node… 👍

Matheus82
7 months ago

Hello Simon, could you create a project using Angular and Capacitor as
npm init @angular

Diego Ribeiro
7 months ago

Nice video! Wich VSCode theme you're using?