One: the environment is built with Mac development, with other systems are similar

  • 1: operating system 10.14.5
  • 2: Mysql local database 5.7.28
  • 3: Node version 10.15.0
  • 4: Express version 4.17.1
  • 5: Log4JS version 6.1.0
  • Mysql 2.17.1 mysql 2.17.1 mysql 2.17.1 Express is developed by TJ Daishen. It is a middleware that encapsulates Connect and provides Web services. It is a powerful tool for developing Web programs. History is always progressing, afraid you have nothing to learn, later he completed koA1, KOA2 design and core development alone. (No more to say) Thunder shock

Two: Node background is built here and installed using NPM

The command is as follows:

 npm install express
 npm install mysql
 npm install log4js
Copy the code

Three: database links

  • Var mysql = require(‘mysql’);
  • 2: Connects to the database
var connection = mysql.createConnection({
    host: 'localhost'// Host port:'3306'// port user:'root'// user name password:'123456'// Password database:'nodeTest'// database name});Copy the code

Four: Log output

  • 1: Import the package we just installed
var log4js = require('log4js');
Copy the code
  • Var log4js = require(‘log4js’); Harpies sometimes

Five: Project launch

Go to the file directory and execute Node main.js

var express = require('express');
var app = express();
var bodyParser = require('body-parser')
app.use(bodyParser.urlencoded({ extended: false})) // We can use uncaughtException to catch uncaught errors globally, and you can also print out the call stack to prevent node from exiting process.on('uncaughtException'.function(err) {// Prints an error console.log(err); // Print out the error call stack for debugging console.log(err.stack); }); // connection.end(); app.listen(3000,function() {//// listen on port 3000 console.log()'Server running main.js at 3000 port');
});
Copy the code

Server running main.js at 3000 port is successful. When deployed online, it needs to be started with PM2, which will be explained when finally deployed.

Ps: Some of the code is screenshot, may not be copied, I will upload the lastgithub, you can download the code hahaha…)