Web application architectures
and introduction to node.js
Seoul National University of Science and Technology
Information Technology Management
Lecture slides index
May 13, 2025
Presentation Layer (Frontend)
Application Layer (Backend / Business Logic)
Data Layer (Database)
Client-side
Server-side
app.js
file in your project directorynpm init
to initialize a package.json
configuration file (you can keep pressing Enter to use defaults)npm install <package-name>
app.js
)app.js
npm install express
to install the Express.js packageconst express = require('express');
const app = express();
const PORT = 8080;
app.get('/posts', function (req, res) {
console.log("Endpoint /posts received a GET request.");
res.type("text").send("Hello World");
});
app.listen(8080, () => {
console.log(`Our first node app listening on port ${PORT}`);
});
Important
package.json:
This file is primarily used for managing and documenting metadata about the project, including its name, version, author, dependencies, scripts, and other configuration details. It acts as a manifest for the project.
package-lock.json
: This file is generated and updated automatically by npm
when installing or updating packages. It is used to lock the exact versions of dependencies installed in the project, ensuring reproducibility and consistent installations across different environments.
.js
file using Node.js, you have access to default functions in JS (e.g. console.log)require()
function, passing the string name of the module you want to import.const
Keywordapp.js
, add the following code - (process.env.PORT is needed to use the default port when hosted on an actual server)// Allows us to change the port easily by setting an environment variable
const PORT = process.env.PORT || 8080;
app.listen(PORT);
Note
Localhost is a hostname that refers to the current computer used to access it. - A hostname is a label assigned to device connected to a computer network - The name localhost is reserved for loopback purposes
GET
and POST
app.get
allows us to create a GET endpoint.req
is the request object, and holds items like the request parameters.res
is the response object, and has methods to send data to the client.res.set(...)
sets header data, like “content-type”.
res.send(response)
returns the response as HTML text to the client.res.json(response)
does the same, but with a JSON object.Name | Description |
---|---|
req.params |
Endpoint “path” parameters from the request |
req.query |
Query parameters from the request |
Name | Description |
---|---|
res.write(data) |
Writes data in the response without ending the communication |
res.send() |
Sends information back (default text with HTML content type) |
res.json() |
Sends information back as JSON content type |
res.set() |
Sets header information, such as “Content-type” |
res.type() |
A convenience function to set content type (e.g., "text" or "json" ) |
res.status() |
Sets the response status code |
res.sendStatus() |
Sets the response status code with the default status text |
app.get('/hello', function (req, res) {
// res.set("Content-Type", "text/plain");
res.type("text"); // same as above
res.send('Hello World 2!');
});
Route path: /majors/:major/courses/:course
Request URL: http://localhost:8080/majors/ITM/courses/WebProg
req.params: { "major": "ITM", "course": "WebProg" }
Route path: /courseInfo
Request URL: http://localhost:8080/courseInfo?major=ITM&course=WebProg
req.query: { "major": "ITM", "course": "WebProg" }
app.get("/courseInfo", function (req, res) {
let major = req.query.major; // ITM
let course = req.query.course; // WebProg
// do something with variables in the response
});
Web Programming