"Node. js" One, build an express framework based on the operating environment + replace the HTML view engine

Source: Internet
Author: User
Tags generator generator

1) Install the Express generator generator

This express generator generator is similar to the VUE-CLI scaffolding tool used to create a backend project, first we will install the generator globally and enter the following command in CMD:

NPM install-g Express-generator

is the Express generator generator installed successfully? Enter the command directly on the command line tool to view the builder version

Express--version

  

2) Automatically create a server project from the generator

After the Express generator generator is installed, build the server project structure by epress Generator built-in commands (you first need to go through the command line CD into the path where you want to store the server project), the command is as follows:

Automatically create a server project Structure Express server/install related packages dependent NPM install

Projects can be generated soon, and the project structure is as follows:

The Models folder inside the-----------------------------------------------------------------was added to me later.

3) Launch our Express framework-based server project with CMD

First enter SERVERCD server//and then under node run the Bin folder under the WWW file node bin/www

Open the browser, enter localhost:3000, see, it means that the express service started successfully.

4) Replace the Jade engine with HTML

Note that under the View folder, there are three. Jade type files, express using the Jade engine by default (or recommend that you use Jade,jade syntax or very simple and efficient), we do not use the Jade template language, can be changed to HTML, the following changes

First step : First remove the three. Jade files below the Views folder

Step two : via CD. command, return to the server first directory, and then install Ejs by command

NPM Install Ejs--save

Step three : After installing Ejs, know app.js, introduced in front Ejs

var ejs = require ('ejs')

Fourth Step : Under the App.js file, find App.set (' View engine ', ' Jade '), put this line of code annotation, and then append the following two lines of code

App.set (' View engine ', ' Jade '); App.engine ('. html ', ejs.__express)//Set the view engine suffix to. Htmlapp.set (' View engine ', ' html '); Set the view engine to HTML

Fifth Step : Create a new file named Index.html under the Views folder

Index.html inside the content with you, mine is as follows:

Sixth Step : Open the browser, enter localhost:3000, the effect is as follows:

The view engine was replaced successfully!

---end---

"Node. js" One, build an express framework based on the operating environment + replace the HTML view engine

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.