![]() You will see later other additions that you can add to your cURL requests. Depending on the HTTP method you are choosing, you will access different routes of your Express application - which here represent only a single API endpoint with an URI so far. However, you can specify the HTTP method with the -X flag (or -request flag). Add the following routes to your Express application whereas the URI itself doesn't change, but the method used from your Express instance:īy default cURL will use a HTTP GET method. Let's set up more routes to accommodate a RESTful API for your Express application eventually. Previously you have already implemented one Express route, which sends a "Hello World!", that you have accessed via the browser and cURL. REST API) to communicate as a client with your server application. If you want to have an alternative for cURL which works in the browser, check out Postman or Insomnia.Įxpress Routes: HTTP Methods are REST OperationsĮxpress is a perfect choice for a server when it comes to creating and exposing APIs (e.g.Get yourself more familiar with the terms client/server and frontend/backend.You will see in the next sections how to use cURL to verify your Express application's REST API, that we are going to implement together, on the command line instead of in the browser. Both tools act as clients whereas the Express application is your server. Whether you access your Express application on in the browser or via the command line with cURL, you should see the same result. If you don't have Homebrew, install it with the following command on the command line: In this guide, we will use Homebrew to install it. For now, the installation guide is for MacOS users, but I guess by looking up "curl for windows" online, you will find the setup guide for your desired OS (e.g. The definition taken from Wikipedia says: "cURL is a computer software project providing a library and command-line tool for transferring data using various protocols." Since REST is an architecture that uses HTTP, a server that exposes a RESTful API can be consumed with cURL, because HTTP is one of the various protocols.įirst, let's install it one the command line. If you haven't heard about cURL, this section gives you a short excursus about what's cURL and how to use it to interact with (REST) APIs. Read more about REST APIs and other APIs.In the following, we are going to implement such REST API for our Express server application, but first let's get to know the tooling that enables us to interact with a REST API. Servers that don't follow the REST architecture a 100% are rather called RESTish than RESTful. A server application that offers a REST API is also called a RESTful server. It's an architecture that leverages the HTTP protocol to enable communication between a client and a server application. However, the most popular one is called REST defined in 2000 by Roy Fielding. How do client and server applications communicate with each other? Over the years, there existed a few popular communication interfaces ( APIs) between both entities. That's the theory behind client-server architectures and how to relate to them. If you want to answer the client-server question if someone asks you what role an entity plays in a client-server architecture, always ask yourself who (server) is serving whom (client) and who (client) consumes whom's (backend) functionalities? Whereas a frontend application is usually something seen in the browser, a backend usually performs business logic that shouldn't be exposed in a browser and often connects to a database as well. These terms cannot be exchanged that easily. However, don't mistake client application always for frontend and server application always for backend here. Both entities result in a client-server architecture (frontend and backend relationship) whereas the backend would be needed for (A) business logic that shouldn't be exposed as source code to the frontend application - otherwise it would be accessible in the browser - or for (B) establishing connections to third-party data sources (e.g. Part 2: How to setup Express.js in Node.jsĪn Express application is most often used as a backend application in a client-server architecture whereas the client could be written in React.js or another popular frontend solution and the server could be written in Express.Part 1: The minimal Node.js with Babel Setup.This tutorial is part 3 of 3 in this series.
0 Comments
Leave a Reply. |