Chap 6 Expressintro
Chap 6 Expressintro
✔ It's a layer built on the top of the Node js that helps manage servers and
routes
● Express was created to make APIs and web applications with ease,
● It saves a lot of coding time almost by half and still makes web and
● Time-efficient
● Fast
● Economical
● Easy to learn
● Asynchronous
Features of Express JS
● Middleware
● Routing
● Templating
● Debugging
Express makes it easier as it identifies the exact part where bugs are
Advantages of Using Express With Node.js
Limitations of Express JS
● Netflix
● IBM
● ebay
● Uber
.
To install Express JS on your system first you need to install node js then we will
write a command to install express in the terminal.
Now let’s write our first basic program in which we will print “Welcome To
Simplilearn” on the server.
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.
ExpressJS - Routing
Web frameworks provide resources such as HTML pages, scripts,
images, etc. at different routes.
The following function is used to define routes in an Express application
−
app.method(path, handler)
This METHOD can be applied to any one of the HTTP verbs – get, set,
put, delete. An alternate method also exists, which executes
independent of the request type.
Path is the route at which the request will run.
Handler is a callback function that executes when a matching request
type is found on the relevant route. For example,
var express = require('express');
var app = express();
app.listen(3000);
If we run our application and go to localhost:3000/hello, the server
receives a get request at route "/hello", our Express app executes
the callback function attached to this route and sends "Hello
World!" as the response.
We can also have multiple different methods at the same route. For
example,
var express = require('express');
var app = express();
app.listen(3000);
Routers
Defining routes like above is very tedious to maintain. To separate the
routes from our main index.js file, we will use Express.Router. Create a
new file called things.js and type the following in it.
var express = require('express');
var router = express.Router();
1 GET
The GET method requests a representation of the specified
resource. Requests using GET should only retrieve data and should
have no other effect.
2 POST
The POST method requests that the server accept the data
enclosed in the request as a new object/entity of the resource
identified by the URI.
3 PUT
The PUT method requests that the server accept the data enclosed
in the request as a modification to existing object identified by the
URI. If it does not exist then the PUT method should create one.
4 DELETE
The DELETE method requests that the server delete the specified
resource
ExpressJS - URL Building
We can now define routes, but those are static or fixed. To use the
dynamic routes, we SHOULD provide different types of routes. Using
dynamic routes allows us to pass parameters and process based on
them.
Here is an example of a dynamic route −
var express = require('express');
var express = require('express');
var app = express();
you can use the req.params object to access all the parameters you
pass in the url. Note that the above 2 are different paths. They will never
overlap. Also if you want to execute code when you get '/things' then
you need to define it separately.
// Single routing
var router = express.Router();
app.use(router);
app.listen(PORT, function(err){
if (err) console.log(err);
console.log("Server listening on PORT", PORT);
});
Example 2: Filename: index.js
var express = require('express');
var app = express();
var PORT = 3000;
// Multiple routing
var router1 = express.Router();
var router2 = express.Router();
var router3 = express.Router();
app.listen(PORT, function(err){
if (err) console.log(err);
console.log("Server listening on PORT", PORT);
});
Example 2: Filename: index.js
var express = require('express');
const e = require('express');
var app = express();
var PORT = 3000;
app.listen(PORT, function(err){
if (err) console.log(err);
console.log("Server listening on PORT", PORT);
});
Output:
Now open your browser and make GET request
to http://localhost:3000/student/profile/12/17, now you can see the
following output on your console:
Server listening on PORT 3000
Starting Page: 12
Ending Page: 17