RapidAPI Profile picture
Jan 26 β€’ 21 tweets β€’ 6 min read
Let's break down the process of building a REST API from scratch using Node.js and Express.

Thread πŸ§΅πŸ‘‡πŸ»
Head over to the following thread if you're not aware of REST API.

We are assuming you have Node.js installed on your machine. If you haven't installed it, click on the following link and install it simply.

nodejs.org/en/
Let's start; create an empty directory and initialize your project by running the following command. Initialize the project with npm.
The next step is to create an empty JavaScript file (File name could be anything, in this case, it's index.js) and install Express.

πŸ”— npmjs.com/package/express

Express is a minimal and easy-to-learn framework for Node. Install express framework for Node.
Now that everything is ready, it's time to code our actual API.

Let's start the server first.

In the below code snippet, we are initializing our `app` and starting the local server at port 3000.

Run `node index.js` to start the server. Initialize the app and start the server at port 300.
For more context, listen() function is used to establish the connection on specified host and port.

It takes two params:

- The first is the port number or host.

- The second (optional) is a callback function that runs after listening to a specified host or value.
Moving forward, let's try to access "localhost:3000" in the browser and see what we are getting.

We are getting a "404 Not Found" response which is correct as we haven't defined any endpoints yet. Getting 404 response on localhost:3000.
It's not recommended that you use the browser to build or debug APIs due to security reasons.

We prefer the Paw, a full-featured HTTP client.

Go ahead and download it.

πŸ”— Paw.cloud Paw, a full-featured HTTP client.
As now our server is running successfully at port 3000, let's create an endpoint. πŸ‘‡πŸ»
The `get` method allows us to create HTTP GET requests.

It accepts two params:

- The first is path/route.

- The second is a callback function that handles the request to the specified route. get method accepts two parameters.
The callback function itself accepts two arguments:

- The first is the request which is the data to the server.

- The second is the response which is the data to the client. The callback function itself accepts two arguments:  - The f
Suppose you want to display all the users whenever the client requests the "/users" endpoint.

To return the data from the server to the client, we have the `send` method.

In the code snippet below, we send an array of objects with `name` and `id` fields. Get /users endpoint.
Perfect!

Let's restart the server by running the `node index.js` command and see what we are getting in the Paw. Accessing /users endpoint.
You can make as many endpoints as you want using the same technique.

For demonstration purpose, let's quickly create a POST request endpoint. πŸ‘‡πŸ»
As POST request is to create or add new data to the server. We first need to add middleware so that we can parse JSON body. Add express.json middleware.
We are defining a POST endpoint at the "/user/3" route.

We implemented the logic of throwing a "400 Bad Request" status code if the user forgets to pass the name value in the request body. πŸ‘‡πŸ» POST endpoint.
Let's try to access this endpoint now. πŸ‘‡πŸ»
As you can see, we are getting a response. πŸŽ‰

Great! You can add as many endpoints as want.
If you want to learn about API in-depth, make sure to check completely free RapidAPI courses.

RapidAPI.com/courses?utm_so…
And with that being said, we hope you like this thread.

Follow @Rapid_API to keep up with the great API-related content. πŸ’™πŸ™

β€’ β€’ β€’

Missing some Tweet in this thread? You can try to force a refresh
γ€€

Keep Current with RapidAPI

RapidAPI Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @Rapid_API

Jan 27
API development becomes a little easier if you know the correct terms and their correct meaning.

Here, we complied with widely used terms in API development.

Thread 🧡
πŸ“Œ API Key

An API key is a unique code that lets you call an API.

The only rule is that these keys must be unique and not easy to guess.

Generally, API keys contain lower case and upper case letters with numbers.
πŸ“Œ API Token

The API token is a unique identifier of an application requesting access to your API.

An API token is a form of authentication similar to a username/password.
Read 17 tweets
Jan 26
A brief introduction to API Testing. πŸ§ͺ

API testing is performed to test whether a particular API meets pre-defined parameters or not.

Let's talk more about API Testing πŸ§΅πŸ‘‡πŸ»
API testing includes testing APIs in isolation to ascertain if they meet the functionality, reliability, latency, performance, security, and other essential parameters.
API testing commonly includes testing APIs with JSON or XML payload sent over HTTP, HTTPS, JMS, and MQ. These are widely used data formats and networking/messaging protocols.
Read 13 tweets
Jan 24
What exactly is an API Key?

Thread πŸ§΅πŸ‘‡πŸ»
Let's first talk a bit about APIs.

API is an interface that allows the software to communicate via the internet. In simple terms, APIs are how apps talk to each other.
To call those APIs, you need an API key to establish a communication path between software.

An API key is a unique code for every user which lets you call an API.
Read 15 tweets
Jan 24
Top five APIs that can help you build weather applications. 🌦️

Thread πŸ§΅πŸ‘‡πŸ»
1️⃣ Visual Crossing Weather

Visual Crossing Weather API provides instant access to both historical weather records and weather forecast data.

rapidapi.com/visual-crossin…
2️⃣ Open Weather Map

Get weather and weather forecasts for multiple cities.

rapidapi.com/community/api/…
Read 7 tweets
Jan 20
There are 9 HTTP request methods:

- GET
- PUT
- POST
- HEAD
- TRACE
- PATCH
- DELETE
- OPTIONS
- CONNECT

Let's talk about them in a bit more detail πŸ§΅πŸ‘‡πŸ»
HTTP request methods are the actions initiated from the client-side to perform certain actions.

These HTTP request methods are sometimes called nouns or referred to as HTTP verbs.
πŸ“Œ GET

The GET method is the most common of all these request methods. It is used to get the desired resources from the server.

The GET methods don't affect the state of the server.
Read 12 tweets
Jan 19
Millions of websites are using API somehow in today's era.

It's imperative to optimize your API to provide a better user experience.

Here are five ways to optimize your API performance and make it stand out. πŸ§΅πŸ‘‡πŸ»
1️⃣ Use rate-limiting strategy

There might be a case where the user executes a loop, causing many API requests that can slow down your API performance.
In order to avoid such scenarios, use rate-limiting factors and limit the specific number or request in the given time period.
Read 11 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(