Project background

Recently wrote an article about the koa various middleware application portal Thinking about a specific application, to be able to do a small project, is the ability to exercise oneself, and on the other hand also hope to have some help to you, of course not satisfied please gently gush πŸ˜‚, now has been basically completed, share with everyone;

The overall usage technology of the project is around KOA and mysql; Some other peripheral libraries have been added; For example, mysql ORM Sequelize; Koa’s body parsing middleware KoA-Body and so on…. The project is now on the Github portal

Project installation

This project relies on mysql so it needs to be installed in advance; For installing Windows, I recommend you to take a look at this article. I hope it will be helpful for you.

An important concept of mysql that is highly recommended for those of you who are not familiar with connection pooling is to take a look at portals;

Relational Mapping (ORM) does not write SQL database operations, and maintains tables as objects. The Sequelize ORM framework portal used in this project

After installing and understanding the above concepts, clone the project and modify the database configuration under part-time/config/index.js; NPM install is then executed to install the associated dependencies;

Project launch:

npm run dev # Test environment
Copy the code
npm run prd # Production environment
Copy the code

Document view

At the beginning of the project, I was still thinking about swagger or not, but I was lazy. Under the project config, there is a part-time. Json file saved when the author debugs the interface, you can use postman interface debugging tool to import this file. You can see all the interfaces in the project, and if you don’t know about this, it’s already there for you; portal

An overview of the interface

Project description

Preface:

If you look at the code without knowing the business, you are either a god or a slag. Therefore, I think it is necessary to introduce this project.

This project is not just a head shot. The author bought a set of UI design drawing on a certain fish before, and the business scenes in it are all figured out according to the design drawing. Of course, there are a lot of unreasonable places, and even some functions are not realized at all. PSD is at the end of the article, and I will work out the front end later; Of course, you can do it yourself if you’re interested; If it involves copyright, please contact me to delete it;

Business:

The project is a part-time app application. Currently, the modules are Baodian, Door task, user feedback, internship, part-time job and user. Personally, I think the business coupling degree between internship and part-time job is relatively high, so I deal with them in a unified way. Due to its small size, it can also be used as a new technology to learn and use;

Project Contents:

β”œβ”€ Bin Project Start up File β”œβ”€ Config Project Configuration File (Postman interface document, Database Configuration etc.) β”œβ”€ Controller β”œβ”€lib β”‚ β”œβ”€ β”œβ”€db.js β”‚ β”œβ”€ Utils.js Tool functions β”‚ β”œβ”€ Validator.js Parameter β”œβ”€ Middlewares Middleware (this project just did auth) β”œβ”€ Models β”œβ”€public Static file, β”‚ β”œβ”€ images β”‚ β”œβ”€routes β”‚ β”œβ”€services β”‚ β”œβ”€views β”‚ β”œβ”€app.js β”‚ β”œβ”€ packageCopy the code

Details:

  • Return data processing
/lib/helper.js
Copy the code
class JSONResolve {
  success(msg = "success", code = 200) {
    return {
      msg,
      code,
    };
  }

  json(data, msg = "success", code = 200) {
    return{ msg, code, data, }; }}module.exports = new JSONResolve();
Copy the code

Return controllable success data by calling success or JSON methods; Error processing middleware is used for uncontrolled data return;

/app.js
Copy the code
app.use(
  jsonError({
    // By default, the stack is also sent to the client, which is obviously not reasonable, so a simple layer of processing is done
    postFormat(e, { stack, ... rest }) {
      const customRet = {
        msg: rest.message,
        code: rest.status,
      };
      return process.env.NODE_ENV === "production"? customRet : { stack, ... rest, e }; }}));Copy the code

If you need to manually throw an exception, you can use ctx.throw(500, ‘server handling error ‘); Custom errors such as this format throw;

  • Unified Route Registration
/router/index.js
Copy the code
const fs = require("fs");
const path = require("path");

module.exports = {
  routing(app) {
    const files = fs.readdirSync(__dirname);
    files.forEach((item) = > {
      if (item === "index.js") return;
      const route = require(path.resolve(__dirname, item)); app.use(route.routes()).use(route.allowedMethods()); }); }};Copy the code

In index.js, read other routes through the fs path module (don’t forget to export other files to the router), and then put them in app.js for unified registration;

Partial display of design drawings

Again, the author bought the PSD in a certain fish before, and I put it at the end of the article. If it involves copyright, please contact me to delete it.

If you do front-end projects based on this backend, it is recommended to leave obvious test marks. I will release vUE or React version of the front-end projects later when I have time.

The last

Writing their own projects can be said to have long thought, the author is self-taught, very dishes; When I watched the video before, I always followed the video, and my own ideas could not be put into practice. It was more like project transcription for me, so I always wanted to do a project by myself. Now the idea is also very simple, is behind can use their own front-end technology to do the project, no longer afraid of no interface, no design and so on excuses, finally caused all things are grammar, and then can not practice for a long time, finally forget πŸ˜‚;

I put the project out in the hope that people like me can grow, and I hope to point a little star if it helps you

Design portal extraction code: A9YO