Rapid Profile picture
Jan 26, 2022 β€’ 21 tweets β€’ 6 min read β€’ Read on X
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 Rapid

Rapid 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

Apr 24, 2023
Common REST API challenges.

A thread 🧡
REST APIs adhere to REST(Representational State Transfer) architecture, which comprises several rules that define scalable and lightweight API design.

Here are the biggest challenges faced when working with them πŸ‘‡
1️⃣ Security

APIs are integral to businesses on today’s web and require protection from attackers.

Security methods include encryption, API Keys, OAuth, input validation, error handling, and more.

Security is a deep topic. Check out the OWASP API security guides to know more.
Read 7 tweets
Apr 24, 2023
HTTP headers for CORS explained.

A thread πŸ§΅πŸ‘‡
Let's start with HTTP request headers:

πŸ“Œ Origin

The browser adds this header to CORS requests to indicate the origin domain.
πŸ“Œ Access-Control-Request-Method

Specific CORS requests that affect user data will initiate with a preflight request. This header is added to the preflight to indicate the method used in the main request. For example:

Access-Control-Request-Method: PUT
Read 10 tweets
Apr 24, 2023
10 HTTP headers that developers should be aware of.

ThreadπŸ§΅πŸ‘‡
1️⃣ Accept

A client-side header that defines the type of data the client expects from the server.

This means the server won’t waste time returning a data type the client cannot use.
2️⃣ Authorization

A client-side header that passes the credentials to the server.

This is not the same as authentication. The server can only authenticate the user after it checks the authorization credentials.
Read 12 tweets
Apr 21, 2023
API. What is it?

- What is an API
- Usage of APIs
- Types of APIs
- Benefits of APIs

Thread πŸ§΅πŸ‘‡
πŸ“Œ Application Programming Interface (API)

APIs are a set of protocols, routines, and tools that enable software to communicate with each other.

They allow developers to access and use the functionality of another system without having to understand its inner workings.
For example, when you use a weather app on your phone, it likely relies on an API to retrieve data from a weather service.

The app developers don't need to know how the weather service works internally. They just need to use its API to get the data they need.
Read 8 tweets
Apr 21, 2023
What is the difference between URI, URL, and URN?

A Thread πŸ§΅πŸ‘‡
πŸ“Œ URI

URL (Uniform Resource Identifier) is a unique string of characters used to identify resources on the web.

Typical URIs contain various elements such as a path, authority, query, and fragment. They are structured like this:

scheme:[//authority]path[?query][# fragment]
URIs are only able to identify resources on the internet and not locate them.

Because of this, they are not seen very frequently on the web.
Read 10 tweets
Apr 21, 2023
What is an API?

Thread πŸ§΅πŸ‘‡

{ 1 / 5 } What is an API?
{ 2 / 5 } The API call is initiated. ...
{ 3 / 5 } Our worker ant acts like an...
Read 6 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

Don't want to be a Premium member but still want to support us?

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!

:(