Make a RESTFul API in nodejs using ExpressJS
Hello viewers, whats up!! Here i am back again with the new series of Nodejs. Node.js is an open-source, cross-platform JavaScript runtime environment that allows developers to run JavaScript code outside of a web browser. It is built on the V8 JavaScript runtime engine, which is the same engine that powers the Google Chrome browser.
Node.js is commonly used for building APIs (Application Programming Interfaces). You can create RESTful APIs or GraphQL APIs using Node.js to handle HTTP requests and serve data to client applications.
Key features of Node.js include:
# Asynchronous - Node.js is designed to be asynchronous, meaning it can
handle a large number of concurrent connections without the need for
multithreading.
# Single-threaded, Non-blocking I/O -
Node.js uses a single-threaded event loop to manage asynchronous
operations.
# npm (Node Package Manager): npm is the default
package manager for Node.js, allowing developers to easily manage and install
libraries and dependencies for their projects.
# Server-Side Development: Node.js is commonly used for server-side development, where it excels at
handling real-time, data-intensive applications, such as chat applications,
online gaming, and streaming services.
But Why we are using Expressjs Here, just because of incase if
you want to make API using nodejs directly then
its a very complex method to create API and integrate directly thats why we
are using Expressjs framework and in this framework with the help, you
can create RESTful API easily.
Install ExpressJS
npm i or npm install express - with this help you can install ExpressJS framework.
RESTFUL API
Importing the express module:
const express =
require('express');
This line imports the Express framework into your script. express
is a web application framework for Node.js that simplifies the process of
creating web servers and handling HTTP requests and responses.
Creating an Express application instance:
const app = express();
This line
creates an instance of the Express application. This app object is the core of
your web server and is used to define routes, middleware, and other
settings.
Setting the port number:
const port = 5000;
Here, the variable port is set to 3000. This is the port on which
your server will listen for incoming requests. You can change this port number
to any other valid port number.
Defining a simple route:
app.get('/api/hello', (req, res)
=> {
res.json({ message: 'Hello, API!' });
});
This code defines a route for handling HTTP GET requests to the
path /api/hello. When a request is made to this endpoint, the server responds
with a JSON object containing a message property: {"message": "Hello, API!"}.
The res.json() method sends a JSON response to the client.
Starting the server:
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
The app.listen() method starts the server and makes it listen for
incoming requests on the specified port (3000 in this case).
The
callback function is executed once the server has started, and it logs a
message to the console indicating that the server is running.
To
run this code, you would save it in a file (e.g., index.js or restful-api.js )
and execute it using the node command in the terminal:
After running the script, you can open a web browser or use a tool like
Postman to make a GET request to http://localhost:5000/api/hello and
see the JSON response.
Whole Code
const express = require('express') const app = express(); const port = 5000; app.get('/api/hello', (req, res) => { res.json({ message: 'Hello, API!, I am atul from kumaratuljaiswal.in' }); }); app.listen(port, () => { console.log(`Server is running on port ${port}`); });
# Install redux and saga packages - CLICK HERE
# Make reducer wrapper - CLICK HERE
# Action in reducer - CLICK HERE
# Reducer in redux -
CLICK HERE
# Switch Stmt in redux -
CLICK HERE
# Get data in component from redux - CLICK HERE
# Remove from cart - CLICK HERE
# Add Redux Toolkit in react redux saga - CLICK HERE
# Configure MiddleWare saga - CLICK HERE
# Call API with Saga and Set Result in react redux saga - CLICK HERE
# Product list ui with API data in react redux saga - CLICK HERE
# Remove to Cart with ID react redux saga - CLICK HERE
# Add Routing and Make Cart Page - CLICK HERE
# Show Added To Cart Product with Price Calculation -
CLICK HERE
Disclaimer
0 comments:
Post a Comment
For Any Tech Updates, Hacking News, Internet, Computer, Technology and related to IT Field Articles Follow Our Blog.