Introduction
Node.js (Node.js in Italian) is an open source runtime environment that allows the execution of JavaScript code on the server side. It is ideal for building scalable and efficient web applications.
Prerequisites
Have Node.js and npm (Node Package Manager) installed on your computer. If you haven't already installed them, you can download and install them from the official Node.js website.
Creating a new Node.js application
Create a new folder for your project:
mkdir miaAppNode
cd miaAppNode
Initialize a new Node.js project with npm init
. This command will create a new package.json
file in your directory, which contains information about your project and its dependencies.
npm init -y
Installing Express.js
Express.js is a framework for Node.js that makes it easy to build web applications.
Install Express.js with npm:
npm install express --save
Creating a simple web server
Create a new file called app.js
in your project directory.
Open app.js
in a text editor and add the following code:
const express = require('express');
const app = express();
const port = 3000;
app.get('/', (req, res) => {
res.send('Hello World!');
});
app.listen(port, () => {
console.log(`Applicazione in esecuzione su http://localhost:${port}`);
});
Save the file and start the server with Node.js:
node app.js
Go to http://localhost:3000
in your web browser. You should see "Hello World!" written.
Conclusion
Congratulations! You've just built your first web application with Node.js and Express.js. From here, you can start exploring how to build more complex applications, manage data persistence with a database, authenticate users, and much more.