Javascript Node.js Pemrograman

How to Create a REST API using Node JS & Express

API CREATE READ UPDATE DELETE using NODE js

How to Create a REST API using Node.js & Express – Hi bret this time I will share a tutorial on how to make an API using node.js, RESTful API is an implementation of API (Application Programming Interface).

REST (REpresentational State Transfer) is an architectural communication method that uses the HTTP protocol for data exchange and this method is often applied in application development. It may sound complicated but it really isn’t.

The goal is to make a system with good performance, fast, and easy to scale, especially in data exchange and communication.

Below is a table summarizing the main HTTP methods combined with URIs

CRUDHTTPEntire Collection (e.g. /customers)
CreatePOST201 (Created), ‘Location’ header with link to /customers/{id} containing new ID.
ReadGET200 (OK), list of customers. Use pagination, sorting and filtering to navigate big lists.
UpdatePUT405 (Method Not Allowed), unless you want to update/replace every resource in the entire collection.
DeleteDELETE405 (Method Not Allowed), unless you want to delete the whole collection—not often desirable.

In the following table, the rows show the tight relationship between CRUD, HTTP, and REST. To create something new use a POST request and call the REST endpoint /api. To read something use a GET request and the call the REST endpoint /api/{id}. The id is passed as an argument to the endpoint. To update something use a PUT request and call the REST endpoint /api. To delete something use a DELETE request and call the REST endpoint /api/{id}. Use the id parameter to pass the unique identifier of what you want to delete – Create a REST API using Node JS & Express.

Create a REST API using Node JS & Express
How to Create an Express js REST API using Node js

How to create a API Create Read Update Delete using node.js ?

Create a REST API using Node JS & Express (Create, Read, Update, Delete) API using Node.js and Express, you can follow these steps:

1. Install Express Library : npm install express in your project’s directory.

2. Create a new file, with filename server.js, and import Express at the top line : const express = require('express')

3. Create an instance of Express : const app = express()

4. Use a body-parser middleware to parse incoming request bodies: const bodyParser = require('body-parser') and app.use(bodyParser.json())

5. Define routes for your API using the app.get(), app.post(), app.put(), and app.delete() methods.

For example, receiving a GET request and fetching all items:

app.get('/api', (req, res) => {
    // Get all items from the database
    const items = getAllItems();
    res.json(items);
});

6. Initiate a POST request to create a new item :

app.post('/api', (req, res) => {
    // Create a new item using the request body
    const item = createItem(req.body);
    res.json(item);
});

7. Initiate a PUT request to update an existing item :

app.put('/api/:id', (req, res) => {
    // Update the item with the specified ID using the request body
    const item = updateItem(req.params.id, req.body);
    res.json(item);
});

8. Initiate a DELETE request to delete an existing item :

app.delete('/api/:id', (req, res) => {
    // Delete the item with the specified ID
    deleteItem(req.params.id);
    res.sendStatus(204);
});

9. Create a Listening PORT to Start The Server

const port = 3000
app.listen(port, () => {
    console.log(`API listening on port ${port}`)
})

You could now try to test the API by sending a request to the specified route using a tool like Postman or Curl.

Thank you for reading this article regarding How to Create an REST API using Node JS & Express, I hope this is useful and adds to all of our insights 🙂

Read Too :

Avatar

Adin Yahya

About Author

Leave a comment

Your email address will not be published. Required fields are marked *

You may also like

Pemrograman Node.js

Cara Membuat Telegram Bot API

Cara Membuat Telegram Bot API – Halo bret kali ini saya akan share cara membuat bot telegram, Telegram merupakan alat
Pemrograman PHP

Cara Membuat Database MySQL di Phpmyadmin

Membuat Database MySQL di Phpmyadmin – Halo bret !! Kali ini saya akan share cara membuat database MySQL di Phpmyadmin,