Express framework

According to the official introduction, Express is a fast, open and minimalist Web development framework based on node. js platform.

The installation

Express is a Node.js based platform, so make sure you have Node.js installed before installing Express. On the CLI, run node -v to check whether Node.js is installed.

Install Express

First, create a directory for your application, then go to that directory and use it as the current working directory.

E:>mkdir myapp // Create a directory named myapp in the root directory of E:> CD myapp // Go to the myapp directoryCopy the code

Create a package.json file for your app using the NPM init command. This command asks you to enter several parameters, such as the app name, version number, description, and specified entry file. You can simply press Enter and accept most of the default Settings.

E:\myapp>npm init
Copy the code

When we open the generated package.json file, we can see that it contains some initialization information for the project.

Next, install Express in the MyApp directory and save it to the dependency list. On the command line, enter the command NPM install Express –save

E:\myapp>npm install express --save
Copy the code

Note: If you specify –save when installing the Node module, the module will be added to the dependencies list in package.json. NPM install automatically installs all modules listed in the dependencies list.

After the Express installation is complete, you will find a node_modules folder and package-lock.json file under the myapp directory we created. The node_modules folder is used to store all the source files needed for the newly installed Express.

Express application generator

You can quickly create an application skeleton by applying the generator tool Express-Generator. Express-generator includes the Express command line utility. Run the following command to install it:

E:>myapp>npm install express-generator --save
Copy the code

After installation, we open the package.json file and see that we add Express-Generator to the Dependencies list.

Start the first Express project

createexpressproject

To create a project folder at the root of drive E, go to that folder and use the following command to create your Express project.

E:>mkdir project // Create a project in the root directory of E:> CD project // Go to project folder E:\project> Express myFirstExpressDemo Create an Express project called myFirstExpressDemo in the project folderCopy the code

At this point, our first Express project is created.

To open the created project, use the commandnpm installInstall all dependency packages.

E:\project>cd myFirstExpressDemo
E:\project>cd myFirstExpressDemo>npm install
Copy the code

Let’s use the vscode editor to open the project and see what each directory represents. Bin: used to start applications. You can set the startup port number. The default port number is 3000. Public: static resource folder. Routes: indicates the routing file. Equivalent to controller in MVC, express projects created by default include index.js and user.js. Views: view file. It’s the view in MVC. Node_modules: Stores the source files of all dependent packages. App.js: Entry file for the project. Load major dependency packages, configure middleware, load routing, and so on. Package. json: created with the NPM init command, it is used to define the various modules required by the project, as well as the configuration information of the project (such as name, version, license, etc.). Based on this configuration file, the NPM install command automatically downloads the required modules, that is, the runtime and development environment required to configure the project. Package-lock. json: automatically generated during NPM install to record the source and version of each NPM package actually installed in the current state.

Start the application

On MacOS or Linux, run the following command to start the application:

$ DEBUG=myapp:* npm start
Copy the code

In Windows, run the following command to start the application:

> set DEBUG=myapp:* & npm start  或者
> npm start
Copy the code

After the project is successfully started, we can access it by typing http://localhost:3000 in the browser address bar.

Express connect to database

Before connecting to the database, install MySQL and Navicat. Enter the Express project and type NPM install mysql –save-dev to install the mysql module of Node.js.

> npm install mysql --save-dev 
Copy the code

Create a new database.js file in the Routes folder.

// database.js // Mysql var Mysql = require(' Mysql '); Var pool = mysql.createPool({host: 'localhost', // database address user: 'root', // database user name password: '123456', // database password: Function query(SQL, callback) {pool.getConnection((err, connection) => { connection.query(sql, (err, rows) => { callback(err, rows) connection.release() }) }) } exports.query = queryCopy the code

In the index.js file

Var router = express.router () const db = require('./database') Router. get('/user', (err, res) => {const SQL = 'SELECT * FROM user'; db.query(sql, (err, result) => { if(err){ return; } // res: API data // result: returned data, need to be converted to JSON format res. JSON (result); }); }) module.exports = router;Copy the code

The user table data in the database

Using the Postman tool, the following success example is returned:

What write is not good, still ask everybody great god many correct!