preface

Node.js is a back-end language based on JavaScript. Front-end partners can quickly get started and build a local server themselves. Let’s see how to do it


Note: this article needs to understand the MySQL database add, delete, modify, query commands, need to manually create a warehouse and data table

Node installation and simple use

1. Download

Liverpoolfc.tv: node

  • Download node.js from the node official website and install it.

  • After the installation is successful, open any terminal window, use CMD window (win + R, enter CMD)

  • Enter in the terminal windownode -vIf the Node version is displayed, the installation is successful

2. Easy to use

Note: here use VSCode editor demo, file name can be customized, it is recommended to use English name!

  • Create a new code folder and open it using the code editor

  • In the Workspace, right-click and select Open in Integration Terminal

  • Enter NPM init -y in the integration terminal to quickly initialize the NPM

  • After initialization, a package.json file appears in the workspace, where the downloaded third-party modules are recorded

  • If you contact NPM for the first time, you are advised to run the following command to use taobao image download to speed up the download of third-party modules

    npm config set registry https://registry.npm.taobao.org

  • Next, execute the command to download the required third-party modules

    npm install express mysql

  • After successful download (as shown below)

Second, code demonstration

1. Connect to the database

The code is as follows (example) :

Create db.js to make the code structure clear and reusable, select new file here and connect to mysql database

    / / export
    module.exports = (sql,callback) = > {
        const mysql = require('mysql')
        const conn = mysql.createConnection({
            host:'localhost'.// The user and password must be added manually and must be consistent with the database
            user:' '.password:' '.database:'database name'
        })
        // Establish a connection
        conn.connect()
        conn.query(sql,callback)
        // Disconnect the connection
        conn.end()
    }

Copy the code

2. Create a local service

The code is as follows (example) :

A new index. Js

 // Load express first
    const express = require('express')
    const app = express()
    / / the port number
    const port = 3000
    // Introduce a custom mysql file
    const db = require('./db.js')
    
    // This is just an example of sending GET requests
    app.get('url'.(req,res) = >{
        db('select * from table_name '.(err,result) = > {
            if(err) throw err
            res.send(result)
        })
    })
    
    app.listen(port,() = > console.log('server is start,port is', port))
Copy the code

3. Test the local service

  • The code editor runsindex.js

  • useApiPost Software testing local services

127.0.0.1orlocalhostBoth are local addresses


conclusion

This article does not explain how to create a new database, if you need a demonstration, let me know in the comments section