Home > Article > Web Front-end > nodejs express ejs installation
Node.js is a popular JavaScript runtime environment that allows developers to develop high-performance back-end applications using the JavaScript language. Node.js comes with Express.js, a simple and flexible web application framework, and also provides a powerful template engine EJS, which can help developers create beautiful and easy-to-maintain web applications.
In this article, we will discuss how to install Node.js, Express.js, and EJS and see how to use them together to create a simple web application.
Before you begin, please make sure your computer has the Node.js runtime environment installed. If you haven't installed it yet, please follow the steps below to install it:
After the installation is complete, open a terminal (or command line window) and enter the following command to check whether Node.js is installed correctly:
node -v
If Node.js is installed successfully, it will be displayed Display the version information of Node.js.
After installing Node.js, you can use the npm command (the package manager that comes with Node.js) to install Express.js . Please follow the steps below to install:
Open a terminal (or command line window) and enter the following command:
npm init
This command will guide you to create a new Node .js project. Follow the prompts step by step to enter.
After the initialization is completed, enter the following command to install Express.js:
npm install express --save
This command will install the latest version of Express.js from the npm repository and install it. Add it as a dependency to your project.
app.js
in the root directory of the project. This is the main entry point to your Express.js application. In the app.js
file, enter the following code to create a simple Express.js application:
const express = require('express'); const app = express(); app.get('/', (req, res) => { res.send('Hello World!'); }); app.listen(3000, () => { console.log('Server listening on port 3000!'); });
This application will respond to requests from All HTTP GET requests from the client browser and return a string containing "Hello World!"
After saving the app.js
file, switch to the terminal (or command line window) and switch the working directory to the app.js
file directory and run the following command:
node app.js
If everything goes well, you should see the terminal output "Server listening on port 3000!" This indicates that your Express.js application is running and can be accessed by visiting http://localhost:3000/.
EJS is a simple, easy-to-use template engine that helps you create web pages with dynamic content in Express.js applications. Please follow the steps below to install:
Open a terminal (or command line window) and enter the following command:
npm install ejs --save
This command will install the latest version from the npm repository EJS and add it as a dependency to your project.
views
. This will be where your Express.js application will store its EJS template files. views
folder, create a new file named index.ejs
. This will be the main template file for your web page. In the index.ejs
file, enter the following code to create a simple web page:
<!DOCTYPE html> <html> <head> <title>EJS Example</title> </head> <body> <h1>Welcome to the EJS Example!</h1> <p>The current date and time is <%= new Date().toString() %>.</p> </body> </html>
This template will display a title, "Welcome to the EJS Example!", and a paragraph containing the current date and time.
Back in the app.js
file, add the following code in the header to tell the Express.js application where to find the EJS template files:
app.set('views', './views'); app.set('view engine', 'ejs');
In the app.js
file, replace the app.get
method with the following code:
app.get('/', (req, res) => { res.render('index'); });
This method will be passed through EJS The template engine renders the views/index.ejs
file and sends it back to the client browser.
After saving the app.js
and index.ejs
files, switch to the terminal (or command line window) and switch the working directory to app.js
The directory where the file is located, and run the following command:
node app.js
If everything goes well, you should see the terminal output "Server listening on port 3000!". This indicates that your Express.js application is running and can be accessed by visiting http://localhost:3000/.
Click the link and you should see a web page with the current date and time.
In this article, we learned how to install Node.js, Express.js, and EJS and create a simple web application. In the next article, we’ll dive into how to use Express.js and EJS to create more complex and powerful web applications.
The above is the detailed content of nodejs express ejs installation. For more information, please follow other related articles on the PHP Chinese website!