Node-Express-REST Intro
Node-Express-REST Intro
What is Node.js?
⦁ Node.js is an open source server environment
⦁ Node.js is free
⦁ Node.js runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.)
⦁ Node.js uses JavaScript on the server
Download Node.js
The official Node.js website has installation instructions for Node.js: https://nodejs.org
Getting Started
After installing the node js type in the CLI 'node' and hit ener then the node prompt will open
where you can write javascript codes and execute.
Example:
C:\Users\student>node
Welcome to Node.js v20.9.0.
Type ".help" for more information.
> console.log("Hello World")
Hello World
undefined
Start your command line interface, write node server.js and hit enter:
C:\Users\student>node server.js
Now, your computer works as a server!
If anyone tries to access your computer on port 8080, they will get a "Hello World!" message in
return!
Start your internet browser, and type in the address: http://localhost:8080
1
ExpressJS:
What is Express?
Express provides a minimal interface to build our applications. It provides us the tools that are
required to build our app. It is flexible as there are numerous modules available on npm, which
can be directly plugged into Express.
ExpressJS - Environment
If Node and NPM is installed then Express JS can be used.
After installing the Node & NPM create a folder then navigate to that folder and open CLI on
that folder. After opening the CLI type:
C:\Users\student\my-express>npm init
Now we have our package.json file set up, we will further install Express. To install Express and
add it to our package.json file, use the following command −
C:\Users\student\my-express>npm install --save express
This will start the server. To test this app, open your browser and go to http://localhost:3000
2
and a message will be displayed as in the following screenshot.
RESTful API:
Representational State Transfer (REST) is an architectural style that defines a set of constraints
to be used for creating web services. REST API is a way of accessing web services in a simple and
flexible way without having any processing.
REST Methods
In HTTP there are five methods that are commonly used in a REST-based Architecture i.e., POST,
GET, PUT, PATCH, and DELETE. These correspond to create, read, update, and delete (or CRUD)
operations respectively. There are other methods which are less frequently used like OPTIONS
and HEAD.
⦁ GET: The HTTP GET method is used to read (or retrieve) a representation of a
resource.
⦁ POST: The POST verb is most often utilized to create new resources.
⦁ PUT: It is used for updating the capabilities. However, PUT can also be used to create a
resource in the case where the resource ID is chosen by the client instead of by the
server.
⦁ PATCH: It is used to modify capabilities. PATCH is neither safe nor idempotent.
⦁ DELETE: It is used to delete a resource identified by a URI.
3
REST Api Status Codes
⦁ 1xx: Informational – Communicates transfer protocol-level information.
⦁ 2xx: Success – Indicates that the client’s request was accepted successfully.
⦁ 3xx: Redirection – Indicates that the client must take some additional action in
order to complete their request.
⦁ 4xx: Client Error – This category of error status codes points the finger at clients.
⦁ 5xx: Server Error – The server takes responsibility for these error status codes.
'{"name":"John", "age":30}'