Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade
Express.js is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications. It is particularly known for its simplicity and ease of use, making it an excellent choice for both beginners and experienced developers. On macOS, setting up and running an Express.js server is straightforward and can be done using the Terminal.
In this article, we will guide you through the process of creating a simple web server using Express.js on macOS. We will cover the installation of Node.js and Express.js, the creation of a basic server, and how to run the server via the command line.
Examples:
Install Node.js and npm: First, you need to install Node.js and npm (Node Package Manager) on your macOS. You can download the installer from the official Node.js website (https://nodejs.org/) or use a package manager like Homebrew.
Using Homebrew:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
brew install node
Create a new project directory: Open Terminal and create a new directory for your project. Navigate into this directory.
mkdir my-express-app
cd my-express-app
Initialize a new Node.js project:
Initialize a new Node.js project by creating a package.json
file. You can do this using the following command:
npm init -y
Install Express.js: Install Express.js as a dependency in your project.
npm install express
Create a simple Express.js server:
Create a new file named app.js
in your project directory and add the following code to set up a basic Express.js server:
const express = require('express');
const app = express();
const port = 3000;
app.get('/', (req, res) => {
res.send('Hello World!');
});
app.listen(port, () => {
console.log(`Server is running at http://localhost:${port}`);
});
Run the server: To start the server, run the following command in your Terminal:
node app.js
You should see the message: Server is running at http://localhost:3000
. Open your web browser and navigate to http://localhost:3000
to see the "Hello World!" message.