22 5cosc024w Lect Pt2 Node Tut07(5)
22 5cosc024w Lect Pt2 Node Tut07(5)
PART 02 (NODE)
Support Lecture for Tutorial 07: JavaScript, Node.js & Express.js
Dr Francois ROUBERT
[email protected]
1
Part 02 Tut 07 Supporting Lecture – Outline
4
JavaScript: most used language for web development
6
JavaScript: now also a back-end language
• Be used as utility scripts to configure system and to read, write and manipulate files.
oto use the same language both on the server and client.
11
Node.js: coding, speed & performance
Node.js
• Only one language for back-end and front-end.
Coding • Higher amount of code.
o Requires the express module (i.e. imports it) and creates an express app.
o Specifies callback function invoked every time HTTP GET request with a path ”/”
o Callback function takes a request object and a response object as arguments.
• req: request object, represents HTTP request.
• res: response object, represents response sent by app when it gets HTTP request.
o Responds to a HTTP request of type GET used to request data from a specified URL
and returns the displaying of a string using the send method. 21
Web Template Engine (Tut 07 Task 04)
oSoftware that combines web templates with a data source to generate
HTML by replacing variables in a template file with actual values.
oData source e.g. relational DB, XML file, JSON file, BSON file, etc.
oWeb Template: reusable template specified according to a dynamic
templating language.
oTemplating language e.g. JavaScript, C, Java, JSP, ASP, PHP, etc.
22
Embedded JavaScript Templating (EJS) (Tut 07 Task 04)
oEJS
• One of the most popular template engines for JavaScript.
• It enables to rapidly build web applications that are split into different components.
• It embeds JavaScript code in a reusable web template to generate HTML.
• It replaces the variables in a template file with actual values, and displays this
value to the client.
• It retains the syntax of HTML while allowing data interpolation.
oUsing EJS
• Use the tags <% and %> to create a “scriptlet” that contains the logic.
• Use loops to iterate through and display data.
• Use select statement (if-else) to implement several options.
23
Express.js: POST METHOD (Tut 07 Task 07)
o Specifies callback function invoked every time HTTP POST request with a path ”/”
o Callback function takes a request object and a response object as arguments.
• req: request object, represents HTTP request.
• res: response object, represents response sent by app when it gets HTTP request.
o Responds to a HTTP request of type POST used to request data from a specified form
and returns the displaying of the data entered in the form as console msg on terminal.
o FYI on GET and POST methods, https://expressjs.com/en/guide/routing.html
24
References and Essential Readings
o Brown, Ethan (2020). Web development with Node and Express: leveraging the JavaScript stack, 2nd
Edition, 2020. O’Reilly Media.
o Flanagan, David (2020). JavaScript : The Definitive Guide – Master the World's Most-Used
Programming Language, 7th Edition. O’Reilly Media.
o Herron, David (2020). Node.js Web Development, 5th Edition, 2020. Packt Publishing.
o Olusola, S (2021). How to use EJS to template your Node.js application. Available from
https://blog.logrocket.com/how-to-use-ejs-template-node-js-application/ [Accessed 15 Jan 2023]
25