Dead Simple Chat offers prebuilt Chat that can be added in minutes to any app or website. Can be completely customized and offers powerful JavaScript Chat API and SDK.
Introduction
The fetch API was introduced in the node 18 as available by default.
It provides a method for an easy, logical way to fetch resources asynchronously across the network.
Let's dive deeper into the Node Fetch API
What is Node Fetch API?
The Fetch API provides an interface to connect to the network and fetch resources form the network. If you have use XMLHTTPRequest
it is similar to that but new API provides more powerful and cleaner interface and feature set.
The Fetch API used to be available on the client side only but the new node 18 brought the implementation to the server side as well
Using the Fetch API
It is very easy to use the node fetch API. First check if you are running the latest version on node js with node -v
The fetch API is only available with node 18.
Here is how you can use the fetch API
If you want to use the async
await
key words then
These were some of the basics of using the fetch api. Next we will be creating an express / Node application to explain in details the node fetch api.
Here is what we are going to learn
- Using Express Js and Node Fetch API
- Get Requests with Node Fetch API
- POST Requests using Node Fetch API
- Uploading JSON data using Node Fetch API
- PUT Requests using Node Fetch API
- Delete Request using Node Fetch API
- Uploading a file
- Using Streams
- Accessing Headers and other Metadata
- Checking if fetch was successful /Handling exceptions and Errors
- Using older versions of Node? / Node fetch API library
- Conclusion
Using Express Js and Node Fetch API
let us create an ExpressJs
application.
Let us first check if we are running node 18 or later by typing node -v
on our terminal
I am running v18.13.0
. Now install the express js in our application and create a basic route that returns hello world
and listens on port 3000
the code for index.js looks like
Get Requests with Node Fetch API
Let us create a get request using the Node fetch API and get the data from the server and send it to the user.
We will console.log
the results to our terminal.
Here is our code:
Fetch API using Async Await syntax
our index.js code looks like:
and the results that are on our terminal
What are we doing here:
We are using the fetch API to get data from the jsonplaceholder website then we are logging the data that we receive from the API to the console.
In the next step we will send a POST request using the Node Fetch API
POST Requests using Node Fetch API
In this section we are going to send a POST request to the jsonplaceholder
website with some JSON data
Simple POST request
POST request using the async await syntax:
Here we are sending a simple post request with text. Next let us send JSON data using the Node Fetch API
Uploading JSON data using Node Fetch API
Using the async await syntax:
Here we are sending JSON data with the fetch request. This is the response that we get from the server
Next, let us learn how to send a PUT request using the Node Fetch API
PUT Requests using Node Fetch API
Response received from the server:
Delete Request using Node Fetch API
Next we will look into delete request send using the Node Fetch API
Response received from the server:
Uploading a file
In this section we are going to upload a file using Node Fetch API. Create a text file in the root folder and name it input.txt
Write the below code in the index.js file
here we are uploading to httpbin.org/post
and we get the respose from the server
Using Streams
We are going to be using streams to download the JSON data from using the fetch api.
Accessing Headers and other Metadata
It is quite easy to access headers and other meta data from Node JS Fetch API
response received from the server
Checking if fetch was successful /Handling exceptions and Errors
We are going to check if the fetch was successful.
Using older versions of Node?node fetch API library
If you have older versions of node and want to use the Node Fetch API, you can consider using the Node Fetch library. You can easily npm install
the library and use it in your app
Here are some of our other articles that are a good reading:
- Axios GET and POST examples
- API Protocol types: When to use each with Examples
- API vs SDK: Explained with Examples
- Data Fetching with getServerSideProps and getStaticProps
Conclusion
In this article I have explained how you can use the node fetch api on the server side, along with examples. I hope this helps you in you quest to use the Fetch API on the server
DeadSimpleChat
DeadSimpleChat  as the name implies is a chat application that is easy to use. It has 1-1 Chat and Group Chat along with Chat API and SDK. You explore more about the DeadSimpleChat on its website: https://deadSimpleChat.com