Express.js Application Structure
Desliza para mostrar el menú
To start building with Express.js, you will usually create a minimal application in a single file called app.js. This file is the entry point of your Express.js project. Here is what a minimal Express.js application looks like:
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Hello, Express!');
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
Each part of this file has a specific purpose. The first line, const express = require('express');, imports the Express.js module so you can use its features in your application. The next line, const app = express();, creates an instance of an Express application. This app object is used to define routes and configure your server.
The line app.get('/', (req, res) => { ... }); defines a route handler for HTTP GET requests to the root URL (/). When someone visits your server's homepage, this function runs and sends back the text "Hello, Express!".
Finally, app.listen(3000, () => { ... }); starts the server and tells it to listen for incoming connections on port 3000. The callback function runs once the server is up and running, logging a message to the console.
Express.js applications are typically organized around the main app.js file. As your application grows, you might split your code into multiple files, but the app.js file always serves as the main hub that creates the Express instance, sets up middleware, defines routes, and starts the server.
¡Gracias por tus comentarios!
Pregunte a AI
Pregunte a AI
Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla