This is the 16th day of my participation in the August More Text Challenge. For details, see: August More Text Challenge

First, build the project and integrate log4J to implement the logging system

Detailed introduction of koA2 environment setup and log4J log system

Koa2 with log4JS log monitoring

First, install the plug-in

1. Install the KOA-JWT plug-in

npm install koa-jwt
Copy the code

Example usage:

We only need to use this usage, key is the front-end headers. Authentication token front secret is the key required for decryption, login, when obtaining the token, and decryption, the secret must be consistent. Otherwise the decryption will not work

2. Install the JsonWebToken plug-in

npm install jsonwebtoken
Copy the code

var jwt = require('jsonwebtoken');
var token = jwt.sign({ foo: 'bar' }, 'xiaohe', { expiresIn: '2d' })
Copy the code

Foo: ‘bar’ is the key-value pair to encrypt

Xiaohe is key

ExpiresIn is the duration of the token, which is set to two days

3. Install the Mongoose database

npm install mongoose
Copy the code

implementation

1. Connect to the database

Create a config folder with db.js and index.js inside

Db.js is used to connect to the database and print the connection success or failure

/** * database connection * @auther */ const mongoose = require('mongoose') // mongoose const config = require('./index') // Const log4js = require('.. /utils/log4') // Introduce log4j's logging system // Mongoose connects to the database mongoose.connect(config.url, {useNewUrlParser: true, useUnifiedTopology: True}) const db = mongoose. Connection // Print db connection success db.on('error', () = > {log4js. Error (' * * * * * * 'database connection fails)}) / / print database connection fails the on (' open', () = > {log4js. Info (' * * * * * * database connection success ')})Copy the code

Config. js is the basic file used to configure database information

/ * * * * / module configuration file. The exports = {URL: 'mongo: / / 127.0.0.1:27017 / xiaohe'}Copy the code

Robo3T is installed locally, which is used to do database visualization tool, and the database is mongooseDB

Connect to the database and create the corresponding Xiaohe database

Create the Xiaohe database in demo

In the Collections of the Xiaohe database, create the user table

Click Insert Document to populate the user table

{ "_id" : ObjectId("6118cc7ee27588cca19a8b70"), "state" : 1, "role" : "0", "roleList" : [ "60180b07b1eaed6c45fbebdb", "60150cb764de99631b2c3cd3", "60180b59b1eaed6c45fbebdc" ], "deptId" : [ "60167059c9027b7d2c520a61", "60167345c6a4417f2d27506f" ], "userId" : 1000002, "username" : "admin", "password" : "Admin ", "userEmail" : "[email protected]", "createTime" :" 2021-01-17t13:32:06.381z ", "lastLoginTime" : "The 2021-01-17 T13:32:06. 381 z", "__v" : 0, "job" : "front-end architect", "mobile" : "17611020000"}Copy the code

At this point, the database connection and some preparations are almost complete, and you are ready for the next step

The last

Public number: xiao He growth, The Buddha department more text, are their own once stepped on the pit or is learned

Interested partners welcome to pay attention to me, I am: He young life. Everybody progress duck together

Need source code, welcome to add the public number, and then with me to the source code.