Lecture # 8 - Express - .Js
Lecture # 8 - Express - .Js
js
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
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 app = express();
app.get('/:id', function(req, res){
res.send('The id you specified is ' + req.params.id); }); app.listen(3000);
To test this go to http://localhost:3000/123. The
following response will be displayed.
Output
ExpressJS - URL Building cont’d
You can replace '123' in the URL with anything else and
the change will reflect in the response.
A more complex example of the above is −
var express = require('express');
var app = express();
app.get('/things/:name/:id', function(req, res) {
res.send('id: ' + req.params.id + ' and name: ' + req.params.name); });
app.listen(3000);
To test the above code, go
to http://localhost:3000/things/tutorialspoint/12345.
Output
Pattern Matched Routes
You can also use regex to restrict URL parameter
matching. Let us assume you need the id to be a 5-digit
long number. You can use the following route definition −
var express = require('express');
var app = express();
app.get('/things/:id([0-9]{5})', function(req, res){
res.send('id: ' + req.params.id); });
app.listen(3000);
Output
Pattern Matched Routes
Note that this will only match the requests that have a 5-digit
long id.
You can use more complex regexes to match/validate your
routes.
If none of your routes match the request, you'll get a "Cannot
GET <your-request-route>" message as response.
This message be replaced by a 404 not found page using this
simple route −
//Other routes here
app.get('*', function(req, res){
res.send('Sorry, this is an invalid URL.'); });
Output
Important − This should be placed after all your routes,
as Express matches routes from start to end of
the app.js file, including the external routers you required.
For example, if we define the same routes as above, on
requesting with a valid URL, the following output is
displayed. −
ExpressJS - Middleware
Middleware functions are functions that have access to
the request object (req), the response object (res), and
the next middleware function in the application’s request-
response cycle.
These functions are used to modify req and res objects for
tasks like parsing request bodies, adding response headers,
etc.
ExpressJS – Middleware cont’d
Here is a simple example of a middleware function in action −