0% found this document useful (0 votes)
21 views4 pages

Node-Express-REST Intro

Uploaded by

tanvirsultana352
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views4 pages

Node-Express-REST Intro

Uploaded by

tanvirsultana352
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Node JS:

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

Check If Successfully installed


Goto the Command Prompt and type node -v. If some version information is shown then Every
thing is ok. If some error like 'node' is not recognized is shown then the above step has to follow
again carefully.
C:\Users\student>node -v
v20.9.0

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

Creaing a Server in Node js


Create a Node.js file named "myfirst.js", and add the following code:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('Hello World!');
}).listen(8080);

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

ExpressJS - Hello World


We have set up the development, now it is time to start developing our first app using Express.
Create a new file called index.js and type the following in it.

var express = require('express');


var app = express();
app.get('/', function(req, res){
res.send("Hello world!");
});
app.get('/about', function(req, res){
res.send("This is About page..");
});
app.listen(3000);

Save the file, go to your terminal and type the following.


C:\Users\student\my-express>node index.js

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.

If you goto http://localhost:3000/about then the following message will be shown.

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.

REST Api Message body


The response body contains the resource representation. The server selects an appropriate
representation format based on what the request headers contain. Clients can request
information in XML or JSON formats, which define how the data is written in plain text. For
example, if the client requests the name and age of a person named John, the server returns a
JSON representation as follows:

'{"name":"John", "age":30}'

REST Api Headers


The response also contains headers or metadata about the response. They give more context
about the response and include information such as the server, encoding, date, and content
type.

You might also like