Node.js - Building a simple web application

4 lug 2023 1 min di lettura
Node.js - Building a simple web application
Indice dei contenuti

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.

Buy me a coffeeBuy me a coffee

Supportaci se ti piacciono i nostri contenuti. Grazie.

Successivamente, completa il checkout per l'accesso completo a Noviello.it.
Bentornato! Accesso eseguito correttamente.
Ti sei abbonato con successo a Noviello.it.
Successo! Il tuo account è completamente attivato, ora hai accesso a tutti i contenuti.
Operazione riuscita. Le tue informazioni di fatturazione sono state aggiornate.
La tua fatturazione non è stata aggiornata.