Home  >  Article  >  Web Front-end  >  nodejs express ejs installation

nodejs express ejs installation

WBOY
WBOYOriginal
2023-05-11 18:35:07727browse

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.

Installing Node.js

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:

  1. Open [Node.js official website](https://nodejs.org/), click the "Download" button, and choose the operation that suits you System version to download.
  2. After the download is completed, double-click the installation package to install. Just follow the steps of the installation wizard.
  3. 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.

Install Express.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:

  1. 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.

  2. 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.

  3. After successful installation, create a new file named app.js in the root directory of the project. This is the main entry point to your Express.js application.
  4. 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!"

  5. 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/.

Install EJS

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:

  1. 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.

  2. In the root directory of the application, create a new folder called views. This will be where your Express.js application will store its EJS template files.
  3. In the views folder, create a new file named index.ejs. This will be the main template file for your web page.
  4. 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.

  5. 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');
  6. 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.

  7. 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.

Conclusion

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn