Node.js is a runtime environment for executing JavaScript code on servers and across different platforms. It is open-source and widely used for developing applications on both the front-end and back-end. Its official package manager, npm, serves as a repository for software packages. Node.js enjoys popularity among developers due to its extensive community of contributors and comprehensive documentation.
Express.js, on the other hand, is a lightweight and flexible web application framework specifically designed for Node.js. It holds the distinction of being the most widely adopted framework for Node.js development. Express.js offers a broad range of features for building web and mobile applications. Additionally, there are various other frameworks built on top of Node.js. The community surrounding Express.js is sizable and provides numerous libraries that address common challenges encountered during application development.
I chose not to install WSL (Windows Subsystem for Linux) on my desktop PC because it significantly consumes CPU power, memory and slows down my computer when running Visual Studio Code. Instead, I opted to use a low-cost Orange Pi and installed Ubuntu on it. Then, I installed Node.js and Express.js on the Orange Pi. I will be utilizing them for front-end and back-end web application examples in the following blogs.
During the installation process, I encountered numerous errors that prevented me from successfully installing Node.js and npm. After a considerable amount of frustration and conducting thorough research and troubleshooting, I discovered that the root cause of these issues was the incorrect configuration of Ubuntu’s date and time settings. Once I rectified and set the date and time correctly, I was able to successfully install Node.js and npm without any further obstacles.
4 libuv1-dev arm64 1.34.2-1ubuntu1.3
404 Not Found [IP: 101.6.15.130 80]
Err:14 http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports focal-security/main arm64 libc-ares2 arm64 1.15.0-1ubuntu0.2
404 Not Found [IP: 101.6.15.130 80]
Fetched 7,968 kB in 7s (1,073 kB/s)
E: Failed to fetch http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/pool/main/libu/libuv1/libuv1-dev_1.34.2-1ubuntu1.3_arm64.deb 404 Not Found [IP: 101.6.15.130 80]
E: Failed to fetch http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/pool/main/c/c-ares/libc-ares2_1.15.0-1ubuntu0.2_arm64.deb 404 Not Found [IP: 101.6.15.130 80]
E: Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?
Installing Node.js and npm
Open a terminal on the Ubuntu system.
Update the package index to ensure having the latest package information:
sudo apt update
Install Node.js and npm (Node Package Manager) using the apt
package manager:
sudo apt install nodejs npm
Verify that Node.js and npm are installed correctly by checking their versions:
node --version
npm --version
Node.js is V10.19.0 and npm is 6.14.4
Installing Express.js
I created a new directory “expressproject” in the home directory for my Express.js project. Then run “npm init” to initialize a new npm project by creating a package.json
file for future applications. Just press Enter to accept the default values.
npm init
Install Express.js in the working directory and save it in the dependencies list. This command will download and install the latest version of Express.js and its dependencies into node_modules
folder under expressproject directory.
npm install express
Once the installation is complete, I can start using Express.js in my projects. Create a new JavaScript file (e.g., app.js
) in the project directory and open it in a text editor, nano. In the app.js
file, it requires the Express module and create an instance of the Express application by adding the following codes. Port 3000 can be changed to match with the firewall settings.
const express = require('express');
const app = express();
const port = 3000
Then I create a simple route ‘/’ that responds with “Hello, World!” and let the app listen to the port. Finally save and close the file app.js.
app.get('/', (req, res) => {
res.send('Hello, World!');
});
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})
To simplify the testing, just remove all firewall rule if there is any by running:
sudo ufw disable
Run the Express application by executing the following command in the terminal:
node app.js
Make sure both Orange Pi and PC have the same subnet and connect to the same router or network switch. On the PC’s web browser, enter the following URL where 192.168.21.26 is the IP address of the Orange Pi.
http://192.168.21.26:3000
Hello Word! I have successfully installed Express.js and created a basic Express application. More examples are coming in the following days.