Node Js Tutorial
- Node JS Introduction
- Node JS Installation
- Node JS Architecture
- Node JS Modules
- Node Js: FILE SYSTEM MODULES
- EVENTS IN NODE JS
- Node JS and Mysql
- Create Application in Node JS
- Global Object in Node JS
- Creating and loading Node Module
- EXTEND EVENT EMITTER
Questions & Answers
RESTFUL API IN NODE JS
An application programming interfaces or is a way for two computers to talk to each other using each other using an API is just like using a website in your browser but instead of clicking buttons and filling out write code to explicitly for example you could visit the nasa website to look at asteroids or you could use their rest api to request the raw json data that is shown screen now most api’s in the world are restful which means they follow a set of rules or constraints known as representational state transfer which has been de facto standard for api development since the early 2000s. A restful api organizes data entities or resources into a bunch of unique url’s well technically not url’s but uri’s or uniform resource identifiers that different types of data resources on a server.
A client gets a data about a resource by making a request to that endpoint over http. The request message has a very specific format.
1) Start line contains the uri that you wish to access which is proceded by an http verb or request method which signal your intent with the resource. A ’get’ request means you just want to read the data and so on as according to following diagram-:
Below the start line there are headers which contain meta data about the request,
– The accept header can tell the server you want the data in a specific format or the authorization header can be used to tell the server that you are actually allowed to make that request.
– Then following headers you have the body which contains a custom payload of data the server will receive the request message then execute some code usually to read from a database that can be then be formatted into a response a message.
The most popular framework for building restful api’s in node is express.js. It’s been around forever and it’s very minimal and easy to learn.
Build a rest API from scratch.
First you need to have JS installed-:
Let’s run ‘npminit y’ from the command line.
This will create package JSon and give us a context for installing packages as-:
That you can use in node like Express. Now let’s install Express using “npm install express”. You will notice that adds express to our dependencies in the package.json file. From there you will need a file to write our code. So, let’s create an index.js file. At the top of the file first declare a variable for app which represents the actual api that you are building and its value is an import of the express package which itself is a function so you will add parentheses to intialize it. And write like this a code for restful API as following -:
Now run the api by using terminal and running node period to run that index.js file. It should have console log.
As if you run it on localhost then you will get “cannot get /
That’s because here you don’t have any api endpoint setup here but express is still responsing with an error message. In fact you can open up the network tab in chrome. If you look closely you will notice that server responded with a 404 status code meaning that page was not found.
At this point you know that api is working but debugging it in the browser is usually not the best option.
Make different ways you can access your api-:
– You can use curl from command line. You could install a vs code extension or you could use a rest client
Now save the file and restart server from the terminal from insomnia, you can make the ‘get’ request to localhost 8080/t-shirt, when you do that you will get Json object as a response body with a status code of 200.
Now let back to code and add a second endpoint this time, a post endpoint as-:
Now let’s go back to insomnia and get POST request to t-shirt endpoint followed by unique id that can be whatever you want. In a body section adds add a request body with a format of json. It contains a logo with a string value that request
You will notice that that get a 500 error response which means our api code is broken. It’s telling us that you have a runtime error because you can’t destructor the property logo from the request body.
Reason is that Express does not parse JSON in the body by default. Not everybody uses express to build a jsonapi so that’s not the default behaviour.
What you need to do here is setup middleware that tells express to parse Json before the actual data hits the function that you are using here to handle the request.
To parse the JSON before the actual data hits function that you are using here to handle the request. When you hear the term middleware think of shared code that runs before every endpoint callback that you have defined. Then the code will be as -:
Now save the file, restart the server and go back to insomnia and if you send the same exact request you will notice the successful response.