Exsp Js
Exsp Js
js 1/36
Web Technologies 1
Back-end server applications with Express.js
Plan
1 Back-end frameworks
HTTP Request
SQL query
HTTP Response
INSA - ASI TW1 – Back-end server applications with Express.js : Back-end server applications with Express.js 3/36
HTTP Request
SQL query
HTTP Response
INSA - ASI TW1 – Back-end server applications with Express.js : Back-end server applications with Express.js 4/36
Back-end frameworks
Why using a framework?
Back-end frameworks
Which one?
Source: [1]
INSA - ASI TW1 – Back-end server applications with Express.js : Back-end server applications with Express.js 6/36
Remarque
Express is not part of the NodeJS APIs
⇒We need to install Express via npm!
output:
require()
INSA - ASI TW1 – Back-end server applications with Express.js : Back-end server applications with Express.js 8/36
listen()
listen()
INSA - ASI TW1 – Back-end server applications with Express.js : Back-end server applications with Express.js 8/36
app. ( )
INSA - ASI TW1 – Back-end server applications with Express.js : Back-end server applications with Express.js 8/36
Request Response
req
res
res.send()
INSA - ASI TW1 – Back-end server applications with Express.js : Back-end server applications with Express.js 9/36
/ExpressJSstatic
/node modules ExpressJSstatic/server.js ...
package.json 5 // enable your express app to serve static files located in
/public ”public” directory
6 app.use(express.static(’public’));
index.html
resource.txt ...
server.js
INSA - ASI TW1 – Back-end server applications with Express.js : Back-end server applications with Express.js 12/36
node_modules
node_modules
INSA - ASI TW1 – Back-end server applications with Express.js : Back-end server applications with Express.js 12/36
node_modules
npm
npm package.json
INSA - ASI TW1 – Back-end server applications with Express.js : Back-end server applications with Express.js 12/36
package.json
package.json
$ npm init
package.json
INSA - ASI TW1 – Back-end server applications with Express.js : Back-end server applications with Express.js 13/36
$ rm -rf node_modules
$ npm install
INSA - ASI TW1 – Back-end server applications with Express.js : Back-end server applications with Express.js 14/36
$ npm
$ npm start
INSA - ASI TW1 – Back-end server applications with Express.js : Back-end server applications with Express.js 15/36
You can not use nodemon directly as a command line but you can
update your package.json so npm can now use nodemon:
...
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon server.js"
},
...
INSA - ASI TW1 – Back-end server applications with Express.js : Back-end server applications with Express.js 16/36
Express
Express is a routing and middleware web framework that has
minimal functionality of its own: an Express application is essentially
a series of middleware function calls
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.
INSA - ASI TW1 – Back-end server applications with Express.js : Back-end server applications with Express.js 17/36
...
8 // endpoint executed for all requests
9 app.use(function (req, res, next) {
10 console.log(req.url);
11 // there is no response sent, we mush call next()
12 next();
13 });
14
15 // endpoint with a specific route
16 app.use(’/home’, function (req, res) {
17 res.send(’Welcome home!’);
18 // we sent a response, no need to call next()
19 });
20
21 // endpoint with a general route
22 app.use(’/’, function (req, res) {
23 res.send(’Hello World!’);
24 // we sent a response, no need to call next()
25 });
...
INSA - ASI TW1 – Back-end server applications with Express.js : Back-end server applications with Express.js 22/36
...
5 // enable your express app to serve static files located in ”public” directory
6 app.use(express.static(’public’));
7 // enable your express app to read request body (for POST requests)
8 app.use(express.urlencoded({ extended: true }));
...
13 // endpoint executed for all requests
14 app.use(function (req, res, next) {
15 console.log(req.url);
16 console.log(req.method);
17 console.log(req.query); // shows GET params
18 console.log(req.body); // show POST params
19 next();
20 });
...
INSA - ASI TW1 – Back-end server applications with Express.js : Back-end server applications with Express.js 22/36
...
22 // endpoint with a specific route
23 app.get(’/data’, function (req, res) {
24 res.send(’GET /data’);
25 });
26
27 // endpoint with a specific route
28 app.post(’/data’, function (req, res) {
29 res.send(’POST /data’);
30 });
...
INSA - ASI TW1 – Back-end server applications with Express.js : Back-end server applications with Express.js 23/36
...
5 //import the fs module
6 const fs = require(’fs’);
...
11 app.get(’/’, (req, res, next) => {
12 fs.readFile(’./file.txt’, { encoding: ’utf8’ }, (err, data) => {
13 if (err) {
14 next(err);
15 } else {
16 res.send(’’+ data);
17 }
18 })
19 })
20
21 app.use((err, req, res, next) => {
22 console.error(err.stack);
23 res.status(500).send(’Something broke!’);
24 })
...
INSA - ASI TW1 – Back-end server applications with Express.js : Back-end server applications with Express.js 25/36
1
See the full list of built-in middleware functions available in express:
https://github.com/senchalabs/connect#middleware
INSA - ASI TW1 – Back-end server applications with Express.js : Back-end server applications with Express.js 26/36
/ExpressJSstatic
/node modules ExpressJSstatic/server.js ...
package.json 5 // enable your express app to serve static files located in
/public ”public” directory
6 app.use(express.static(’public’));
index.html
resource.txt ...
server.js
For instance :
npm install sqlite3
Routing
Routes vs. endpoints
Routing [11]
”Routing refers to determining how an application responds to a
client request to a particular endpoint, which is a URI (or path)
and a specific HTTP request method (GET, POST, and so on).”
Endpoint in
Request method + Route path
Express =
INSA - ASI TW1 – Back-end server applications with Express.js : Back-end server applications with Express.js 30/36
Routing
How to route
Routing
Route paths [13]
Routing
Route paths [13]
2
Again, you can test your route paths using Express Route Tester [12]
INSA - ASI TW1 – Back-end server applications with Express.js : Back-end server applications with Express.js 31/36
Routing
Route paths [13]
2
Again, you can test your route paths using Express Route Tester [12]
INSA - ASI TW1 – Back-end server applications with Express.js : Back-end server applications with Express.js 32/36
Routing
Route parameters
3
Again, you can test your route paths using Express Route Tester [12]
INSA - ASI TW1 – Back-end server applications with Express.js : Back-end server applications with Express.js 33/36
Routing
Response methods
Routing
And when there is no route?
Routing
Serving a 404 error page example: ExpressJS404/server.js
...
13 // basic root route
14 app.get(’/’, function(req, res){
15 res.send(’Hello world!’);
16 });
17
18 // the 404 route (always keep this as the last route)
19 app.get(’*’, function(req, res){
20 res.status(404);
21 //serve a static html file (can be done using express.static)
22 res.sendFile(”404.html”, {root: ”.”});
23 });
...
INSA - ASI TW1 – Back-end server applications with Express.js : Back-end server applications with Express.js 36/36
Conclusion
Key takeaways:
✓ ExpressJS is a powerful (and popular) back-end framework ...
✓ .. built on top of NodeJS, that helps at building complete web
APIs.
✓ In ExpressJS, you define middleware (callback) functions ...
✓ ... for each endpoint of your API.
✓ Routing enables your Express app to handle at the same time ...
✓ ... the requested URL and the request method.
✓ Using NodeJS modules in your Express app enables you to ...
✓ ... decompose your code, routing and middleware in separate files
...
✓ ... in order to create modular and reusable applications.
INSA - ASI TW1 – Back-end server applications with Express.js : Back-end server applications with Express.js 36/36