Node.js

HEIG-VD - WEB Course 2023-2024 - AGPL-3.0 license

Until now, where does the JavaScript code run?

HEIG-VD - WEB Course 2023-2024 - AGPL-3.0 license

Until now, where does the JavaScript code run?

  • In the browser

Wouldn't it be cool to run JavaScript on the server?

HEIG-VD - WEB Course 2023-2024 - AGPL-3.0 license

What is Node.js?

  • Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine.

The user send a request to the server, the server send back a response.

HEIG-VD - WEB Course 2023-2024 - AGPL-3.0 license

Alternatives to Node.js

  • PHP
  • ASP.NET Core
  • Ruby
  • Deno
  • ...
HEIG-VD - WEB Course 2023-2024 - AGPL-3.0 license

Why Node.js?

  • Easy to learn
  • Big community
HEIG-VD - WEB Course 2023-2024 - AGPL-3.0 license

First move

node --version

index.js:

console.log("Hello World!");
node index.js

Hello World!

HEIG-VD - WEB Course 2023-2024 - AGPL-3.0 license

Event loop

process.on("exit", () => {
	console.log("Goodbye!");
});

console.log("Hello World!");
HEIG-VD - WEB Course 2023-2024 - AGPL-3.0 license

callback

process.on("exit", () => {
	console.log("Goodbye!");
});

console.log("Hello World!");

Hello World! Goodbye!

HEIG-VD - WEB Course 2023-2024 - AGPL-3.0 license

Event emitter

const EventEmitter = require("events");
const eventEmitter = new EventEmitter();

eventEmitter.on("start", () => {
	console.log("Started!");
});

eventEmitter.on("end", () => {
	console.log("Ended!");
});

eventEmitter.emit("start");
eventEmitter.emit("end");
eventEmitter.emit("start");
HEIG-VD - WEB Course 2023-2024 - AGPL-3.0 license

Event emitter

Started! Ended! Started!

HEIG-VD - WEB Course 2023-2024 - AGPL-3.0 license

text.txt:

Hello World! I am a text file.
const { readFile } = require("fs");

readFile("./text.txt", "utf8", (err, data) => {
	if (err) {
		console.error(err);
		return;
	}
	console.log(data);
});

console.log("Am I the second message ?");
HEIG-VD - WEB Course 2023-2024 - AGPL-3.0 license

File system

Am I the second message ? Hello World! I am a text file.

HEIG-VD - WEB Course 2023-2024 - AGPL-3.0 license

Module

const { readFile } = require("fs");
HEIG-VD - WEB Course 2023-2024 - AGPL-3.0 license

Create your own module

const add = (x, y) => {
	return x + y;
};

const PI = 3.14159;

module.exports = {
	add: add,
	PI: PI,
};
const math = require("./math.js");
HEIG-VD - WEB Course 2023-2024 - AGPL-3.0 license

NPM

  • Node Package Manager
HEIG-VD - WEB Course 2023-2024 - AGPL-3.0 license

NPM

npm init --yes
.
├── index.js
└── package.json
HEIG-VD - WEB Course 2023-2024 - AGPL-3.0 license
{
	"name": "nodejs",
	"version": "1.0.0",
	"description": "Node.js course",
	"main": "index.js",
	"scripts": {
		"start": "node index.js"
	},
	"keywords": [
		"nodejs"
	],
	"author": "Vincent Guidoux",
	"license": "MIT"
}
HEIG-VD - WEB Course 2023-2024 - AGPL-3.0 license

Install first package

npm install express
HEIG-VD - WEB Course 2023-2024 - AGPL-3.0 license
{
	"name": "nodejs",
	"version": "1.0.0",
	"description": "Node.js course",
	"main": "index.js",
	"scripts": {
		"start": "node index.js"
	},
	"keywords": [
		"nodejs"
	],
	"author": "Vincent Guidoux",
	"license": "MIT",
	"dependencies": {
		"express": "^4.17.1"
	}
}
HEIG-VD - WEB Course 2023-2024 - AGPL-3.0 license

Install dev dependencies

nodemon is a tool that helps develop Node.js based applications by automatically restarting the node application when file changes in the directory are detected.

npm install --save-dev nodemon
HEIG-VD - WEB Course 2023-2024 - AGPL-3.0 license
{
	"name": "nodejs",
	"version": "1.0.0",
	"description": "Node.js course",
	"main": "index.js",
	"scripts": {
		"start": "node index.js",
		"dev": "npx nodemon index.js 3000"
	},
	"keywords": [
		"nodejs"
	],
	"author": "Vincent Guidoux",
	"license": "MIT",
	"dependencies": {
		"express": "^4.17.1"
	},
	"devDependencies": {
		"nodemon": "^2.0.15"
	}
}
HEIG-VD - WEB Course 2023-2024 - AGPL-3.0 license

Create a simple web server

const express = require('express');
const { readFile } = require('fs');

const app = express();

app.get('/', (req, res) => {
  readFile('./home.html', 'utf8', (err, html) => {
    if (err) {
      res.status(500).send('sorry, out of order');
    }
    res.send(html);
  })
});

app.listen(process.env.PORT || 3000, () => console.log('App available on http://localhost:3000'));
HEIG-VD - WEB Course 2023-2024 - AGPL-3.0 license

Dockerize

FROM node:20-alpine as build
WORKDIR /app

COPY package.json package.json
COPY package-lock.json package-lock.json

RUN npm ci --omit=dev

COPY home.html home.html
COPY index.js index.js

EXPOSE 3000

CMD ["npm", "run", "start"]
HEIG-VD - WEB Course 2023-2024 - AGPL-3.0 license

This is a way to make link and shortcut in the code

This comment will center everything on the page