After we discussed about using Node.js with the http module to develop a server, we introduce another Node module, Express.js that we will use to simplify the development activities. We will follow the textbook for the lab activities. We will cover the following lessons in our textbook.
Before we start, I recommend that you read through this article from Mozilla that provides another introduction to Node and Express.
Express.js is not a web server. It is a framework that runs on top of Node’s HTTP server. Under the hood, Express still calls http.createServer()(…)
Express is a downloadable module that can be installed from the command line once with:
1
npm install express --save
At this point, you should familiarize yourself with the source code from the book, which is available freely at. This can be done by:
1
2
3
cd /apps
apt install -y git
git clone https://github.com/JonathanWexler/get-programming-with-nodejs.git
4-express in apps.server-http.js with the following contents:
1
2
3
4
5
6
7
8
9
10
const http = require('http');
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
res.end('Hello from Node HTTP\n');
});
server.listen(3000, () => {
console.log('Node HTTP server listening on port 3000');
});
server-express.js with the following contents:
1
2
3
4
5
6
7
8
9
10
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Hello from Express\n');
});
app.listen(3000, () => {
console.log('Express server listening on port 3000');
});
127.0.0.1:3000 to observe the outputs.main.js using the content from Listing 8.1 in the book (first_express_project_8_3 in lesson_8 from the git repo)req.params, req.body, req.url, and req.query.
This lesson provides a preliminary view on routing/MVC
- Read through Lesson 9 and examine the codes in the
finishfolder oflesson_9to observe how the routes are managed via Express.js- Run
node main.jsto view
Another way to approach this is to start this lesson from the end, which is the 9-3 example in GitHub.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
"use strict";
const port = 3000,
express = require("express"),
app = express(),
homeController = require("./controllers/homeController");
app.use(
express.urlencoded({
extended: false
})
);
app.use(express.json());
app.use((req, res, next) => {
console.log(`request made to: ${req.url}`);
next();
});
app.post("/", (req, res) => {
console.log(req.body);
console.log(req.query);
res.send("POST Successful!");
});
app.get("/items/:vegetable", homeController.sendReqParam);
app.listen(port, () => {
console.log(`Server running on port: ${port}`);
});
express() creates the app object, which is basically a big request router + middleware pipeline.homeController is a custome Node module we build and import. It contains a function to be passed to Express.app.post("/") runs, req.body is already populated (if the request body matches the expected content-type).req = request (incoming data)res = response (how the server replies)next() = I’m done, let the next middleware/route handler run next() will cause the request to hangs.Route: POST / req.body: Results of the body-parsing middleware. For example, sending JSON { “a”: 1 } will activate the parser at line 13 and resulting in req.body.a === 1 req.query: Comes from the URL query string. For example, POST /?debug=true resulting in req.query.debug === "true" res.send(...): Ends the response immediately (sets headers, writes body, closes).Route: GET /items/:vegetable /items/<something> /items/carrot: req.params.vegetable === "carrot" /items/bokchoy: req.params.vegetable === "bokchoy" GET is called, the function sendRequestParam in homeController is activated.
1
2
3
4
5
6
"use strict";
exports.sendReqParam = (req, res) => {
let veg = req.params.vegetable;
res.send(`This is the page for ${veg}`);
};
sendReqParam as a route handler:
9-3: controllers/homeController.js is the Controller layer: res.send(...).First, you are to review the web page at the official website for EJS. It is strongly recommended that you pause and go through the official website to learn more about EJS.
If you are interested in learning more about EJS, here is a good tutorial entitled How To Use EJS to Template Your Node Application.
You can follow this tutorial to develop a complete front-end of a web application with EJS. But it is not required. We will move on and begin to develop our view pages following our textbook in Lesson 10.
Prior to running, need to run the following in terminal:
1npm install express-ejs-layout
Use a layout.js to specify the structure of a web page in your project. Store the layout.js and view files in the same folder, i.e., the views folder.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Recipe App</title>
<link rel="stylesheet" href="./css/custom.css">
<style media="screen">
body {
margin: 0;
padding: 0;
height: 100%;
color: black;
text-align: center;
font-family: 'open sans';
}
#nav {
width: 100%;
text-align: center;
height: 60px;
background-color: #7D498D;
}
#footer {
width: 100%;
text-align: center;
height: 60px;
background-color: #9C73A9;
position: relative;
bottom: 0;
}
#container {
height: 100px;
}
</style>
</head>
<body>
<% include partials/navigation %>
<div id="container">
<%- body %>
</div>
<div id="footer">FOOTER</div>
</body>
</html>
Third, you are to Prepare view files and controllers.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// homeController.js
'use strict';
const express = require( 'express' ),
layouts = require( 'express-ejs-layouts' ),
app = express(),
homeController = require( './controllers/homeController' );
app.set( 'port', process.env.PORT || 3000 );
app.set( 'view engine', 'ejs' );
app.use( layouts );
app.use( homeController.logRequestPaths );
app.get( '/items/:vegetable', homeController.sendReqParam );
app.get( '/name/:myName', homeController.respondWithName );
app.listen( app.get( 'port' ), () => {
console.log( `Server running on port: ${app.get('port')}` );
} );
1
2
3
4
// view file: index.js
<h1> Hello, <%= name %> </h1>
//view file in the partials folder: navigation.js
<div id="nav">TOP NAVIGATION</div>
Here the EJS tag <%= name %> replaces the name by the value of the name into the template while completing the rendering operation.
<% include partials/navigation %>