Using nodejs-koa2-mysql-serialize-jwt to implement project API interface



  • Technology stack: nodejs, koa2, mysql, sequenize, JWT
  • Project data layer and operation layer are clear
  • Using koa2 framework middleware, parameter processing
  • JWT does authority interface verification
  • Sequelize to manage MySQL database
  • Asynchronous processing async / await
  • The login and registration interface has been implemented. The interface is added, deleted, modified and checked
  • Please click star if you like it or help you ✨✨ , or you have better suggestions and opinions, please put forward to me, you can leave a message, you can add me QQ: 841053515, thanks

Click to jump to project address

1、 Learning to use

git clone

git clone

1.1. Installation

npm install

1.2. It needs to be in the config file db.js Configure local database

Const serialize = new Serialize ('Database ',' database user name ',' database password ', {})

Don't forget to create the database. Log in to MSYQL in the black window: create database 'database user name'

1.3. Open service

npm start

2、 Route description

// └──routes/ index.js file

const router = new Router({
    prefix: '/api/v1'

 *User interface
//User registration'/user', UserController.create);
//User login'/user/login', UserController.login);
//Get user information
router.get('/user', UserController.getUserInfo);
//Get user list
router.get('/user/list', UserController.getUserList);
//Delete user
router.delete('/user/:id', UserController.delete);

 *Article interface
//Create an article'/article', ArticleController.create);
//Get article list
router.get('/article', ArticleController.getArticleList);
//Get article details
router.get('/article/:id', ArticleController.detail);
//Delete article
router.delete('/article/:id', ArticleController.delete);
//Change article
router.put('/article/:id', ArticleController.update);

3、 Interface description (user interface)

Create user interface

3.1. Request method


3.2. Request parameters

parameter explain demand
username user name Required
password password Required

3.3. Return data

    "code": 200,
    "MSG": "user created successfully,",
    "data": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6IkJvYiIsImlkIjozLCJpYXQiOjE1Mjg3NzUzOTMsImV4cCI6MTUyODc3ODk5M30.cnWcgJQF1z7adgKp49AgP4UvpqIXUNjGfjWLMq-rMeA"
Login interface



3.5. Request parameters

parameter explain demand
username user name Required
password password Required

3.6. Return data

    "code": 200,
    "MSG": "login succeeded,",
    "data": {
        "id": 3,
        "username": "Bob",
        "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6IkJvYiIsImlkIjozLCJpYXQiOjE1Mjg3NzU0NTIsImV4cCI6MTUyODc3OTA1Mn0.v_B_EXvzYTk7Wz-jl4D8F5n5kn2iah8oht0s6S72Zsc"
Get user information

3.7. Request method


3.8. Description

The token must be passed

Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6IuaigeWHpOazomJvIiwiaWQiOjUsImlhdCI6MTUyNzczNjc2NSwiZXhwIjoxNTI3NzQwMzY1fQ.y5w4lEFRf8bpR4fFPNDms1m9WSX9mfQ3fo5dejG7y3A

3.9. Return data

    "code": 200,
    "MSG": "query succeeded,",
    "data": {
        "id": 3,
        "username": "Bob"
Delete user interface

3.10. Request method


3.11. Request parameters

parameter explain demand
id User ID Required

3.12. Return data

    "code": 200,
    "MSG": "user deleted successfully"

4、 Main documents of the project

4.1.1 schema file

Create database table

4.2.1 modules file

Model layer - main processing parameters

4.3.1 controllers file

Controller processing database addition, deletion, modification and query

4.4.1 router file



Entry file

JWT is used for project authentication, that is, JWT authentication is not required for login registration and obtaining user information, and token authentication is required for other interfaces

For example, register user interface: in the postman software operation interface, for example, the registration interface:

Post request

http://localhost : 3000 / API / V1 / CREATEUSER? Username = Liang Fengbo Bo & password = bobo12345

Information returned after successful creation:

    "code": 200,
    "Message": created successfully,
    "bean": {
        "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6IuaigeWHpOazomJvIiwiaWQiOjUsImlhdCI6MTUyNzczNjUzMSwiZXhwIjoxNTI3NzQwMTMxfQ.GAQg-hZm3rDYq70-16sgfNHvD64gmrWSFzQCZQs7bl4"


Post request

http://localhost : 3000 / API / V1 / user / login? Username = Liang Fengbo Bo & password = bobo12345

Information returned successfully:

    "Message": "login succeeded! "
    "data": {
        "id": 5,
        "Username": Liang Fengbo,
        "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6IuaigeWHpOazomJvIiwiaWQiOjUsImlhdCI6MTUyNzczNjc2NSwiZXhwIjoxNTI3NzQwMzY1fQ.y5w4lEFRf8bpR4fFPNDms1m9WSX9mfQ3fo5dejG7y3A"
    "code": 200

When dealing with JWT validation, I added methods

app.use(jwt({secret: secret.sign}).unless({path: [/^\/api\/v1\/login/, /^\/api\/v1\/createUser/]}))

Login and registration will return token information, except that the two interfaces must send header header

Add token in header

Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6IuaigeWHpOazomJvIiwiaWQiOjUsImlhdCI6MTUyNzczNjc2NSwiZXhwIjoxNTI3NzQwMzY1fQ.y5w4lEFRf8bpR4fFPNDms1m9WSX9mfQ3fo5dejG7y3A

In order to get the interface information, and the token will be invalid after one hour.

Specific can see the main project several file code, learn together progress, if you have any good suggestions or comments, or if there are errors, please leave a message, thank you

Learning recommendation link:

Koa2 realizes JWT authentication_

be based on Egg.js Framed Node.js User management design of service construction

If it is helpful for you to learn nodejs, please give a star ✨✨ thank you