📅  最后修改于: 2023-12-03 14:41:05.234000             🧑  作者: Mango
ExpressJS is a popular web application framework for Node.js. It provides a simple and minimalist approach to building web applications. In this tutorial, we will create a simple "Hello World" application using ExpressJS.
Before starting this tutorial, you should have the following installed on your machine:
Create a new directory for your project and navigate to it in the terminal.
$ mkdir my-express-app
$ cd my-express-app
Then, initialize a new Node.js project using npm.
$ npm init
Follow the prompts to customize your project (you can leave most of the options as default).
Next, install ExpressJS as a dependency for your project.
$ npm install express
Create a new file called index.js
in your project directory.
$ touch index.js
Open the index.js
file in your favorite code editor and add the following code:
// Load the express module
const express = require('express');
// Create a new express application
const app = express();
// Define a route handler for the root path
app.get('/', (req, res) => {
res.send('Hello World!');
});
// Start the server on port 3000
app.listen(3000, () => {
console.log('Server started on http://localhost:3000');
});
This code creates a new Express application, defines a route handler for the root path (/
), and starts the server on port 3000
.
To run the application, execute the following command in your project directory:
$ node index.js
Open your web browser and go to http://localhost:3000
. You should see the message "Hello World!" displayed in the browser.
Congratulations! You have successfully created your first ExpressJS application. ExpressJS is a powerful framework that allows you to build complex web applications with ease. You can now start exploring more of its features and building more advanced applications.