What is Express
Using the official introduction, Express is a minimalist, flexible Web application Development framework based on the Node.js platform that provides a wide range of features to help you create Web and mobile device applications.
How to install
Make sure you have Node.js installed on your machine and then install it via NPM, which is very convenient.
Installation Steps
1. Installation of Express
On the cmd command line, enter the NPM install EXPRESS-G command, and after the carriage return, you will see the following message indicating that the installation was successful.
2. Install Express Application Builder
EXPRESST Application Builder will help us generate express corresponding directory structure, also at the cmd command line, enter the NPM install express-generator-g command, carriage return
As you can see, the Express Application Builder Express-generator has been installed successfully.
3. Create Application
Switch to the workspace root, enter the Express Express-demo command, as shown
The above command creates a Express-demo project application that is located in the D:\javapro workspace. Its directory structure is as follows:
Catalog Description:
/bin: For application startup
List of modules for the/node_modules:web project
/public: Static Resource Directory
/routes: Routing rules, can be understood as controller (Controller)
/views: View, but this view is not a traditional HTML file, but an HTML engine template, where the default template is the Jade file, which can be considered the foreground UI.
App.js: Program main file
4. Installation Dependency
Switch to the Express-demo application root directory, enter NPM install, carriage return
The dependent packages defined in the Package.json file will be installed, as shown in the figure:
Dependency Pack Description:
Body-parser:node.js middleware for processing JSON, Raw, Text, and URL-encoded data.
Cookie-parser: A tool for parsing cookies. Through the req.cookies, you can take the cookie that comes in and turn it into an object.
Jade: A high-performance template engine, which is deeply influenced by Haml, is implemented in JavaScript and can be used by Node.
5. Modify the port number
Node.js the port number used by default is 3000, and if we want to use a different port, we need to modify it manually, and there are two ways to modify it, as follows:
5.1 Modifying App.js files
In the App.js file, add the following code:
App.listen (3001);
After modifying the port in this way, the original 3000 port can also be used for normal access, for unknown reasons.
5.2 Modifying Bin/www files
Change the port number 3000 in the following code to what you want.
var port = normalizeport (Process.env.PORT | | ' 3000 ');
As shown in figure:
6. Start the application
Switch to the project root directory, enter set Debug=express-demo & NPM start or directly enter NPM start, and then start the Express-demo application after the carriage return. As shown in figure:
7. Browser Access
In the browser address bar, enter http://localhost:3000/and you will see the following interface:
Congratulations to you. The express environment was built successfully.