Chapter 9 Express
Chapter 9 Express
Express JS
By Ikhe V. N.
9.1 REST
9.2 Introduction to Express JS
9.3 Routing, Responding
9.4 Configuration
9.5 Views
9.6 Receiving Data
9.7 Error Handling
What is Express?
there are six ground principles laid down by Dr. Fielding who
was the one to define the REST API design in 2000. Below
are the six guiding principles of REST:
1. Stateless
Requests sent from a client to the server contains all the necessary
information that is required to completely understand it. It can be a
part of the URI, query-string parameters, body, or even headers.
The URI is used for uniquely identifying the resource and the body
holds the state of the requesting resource. Once the processing is
done by the server, an appropriate response is sent back to the
client through headers, status or response body.
Client-Server
1. It has a uniform interface that separates the clients from the servers.
Separating the concerns helps in improving the user interface’s
portability across multiple platforms as well as enhance the scalability
of the server components.
The separation between client and server makes it very easy for the
protocol to be used independently for various development projects.
It allows us to build loosely coupled applications, the client and the
server can evolve Separately.
eg: The client and server may not be concerned about which
software they use.
Uniform Interface
First, you need to create your project directory. Next, open the
const books = [
{title: 'Harry Potter', id: 1},
{title: 'Twilight', id: 2},
{title: 'Lorien Legacies', id: 3}
]
app.get('/api/books', (req,res)=> {
res.send(books);
});
app.get('/api/books/:id', (req, res) => {
const book = books.find(c => c.id ===parseInt(req.params.id));
book.title = req.body.title;
res.send(book);
});
res.send(book);
});
function validateBook(book) {
const schema = {
title: Joi.string().min(3).required()
};
return Joi.validate(book, schema);
Run the above example using node app.js command and point your
browser to http://localhost:5000. It will display Cannot GET / because we
have not configured any routes yet.
1. In the above example, we imported Express.js
module using require() function. The express
module returns a function. This function returns an
object which can be used to configure Express
application (app in the above example).
2. The app object includes methods for routing HTTP
requests, configuring middleware, rendering HTML
views and registering a template engine.
3. The app.listen() function creates the Node.js web
server at the specified host and port. It is identical
to Node's http.Server.listen() method.
Configure Routes
res.send()
This function takes an object as input and it sends this to the
requesting client. Here we are sending the string "Hello
World!".
app.listen(port, [host], [backlog], [callback]])
This function binds and listens for connections on the specified host and port.
1) Port :-A port number on which the server should accept incoming requests.
2) Host :- Name of the domain. You need to set it when you deploy your apps to
the cloud.
3) Backlog :- The maximum number of queued pending connections. The default
is 511.
4) Callback :- An asynchronous function that is called when the server starts
listening for requests.
The following example demonstrates configuring routes for HTTP requests.