At the beginning of this article, let’s make a summary. What do we need to do
There may be references to other articles in this article, so please let me know if you are offended
- Build the project
- Familiar with directory structure, code writing specification
- Database installation and use
- Database configuration file
- Interface to write
- We need to build a KOA project quickly, NPM install koa-generator-g then execute koa2 my-project to create our own project then go to the project directory and download the dependencies. NPM install is there and we can run the project NPM start to this project generation is complete http://localhost:3000/
2. What should we do next when the project is set up? Should be need to be familiar with the directory structure, do not know may have a look of www.itying.com/koa/article…
3. I think the most important thing is the database
I learn node most let me be interested in the database should be related to the content, I do not know what you think?
I’m using the mysql download address hereDev.mysql.com/downloads/m… Note that there is a problem with installation-free packages
Go to Download -> Extract -> Install
Once installed, you can use the command line to operate your local database
Navicat Premium 15 can be combined with graphical tools to easily view your database
The next step is to connect to the database
Start by creating a config folder under the directory
├─ config │ ├─ ├─ default. Js Database configuration information │ ├─ mysql.js SQL query as method for calling │ ├─ sql.js as SQL statement
Database: {database: 'XXX ', USERNAME:' XXX ', PASSWORD: 'XXX ', port: '3306', HOST: WxConfig :{APPID: ", AppSecret: "}} module.exports = configCopy the code
Call it using the js method in the Router file
const query = require(".. /config/mysql.js"); Const {SHOW_ALL_DB,QUERY_DATAS} = require(".. /config/sql.js"); Router.get ('/', async (CTX, next) => {let query_res = await query(QUERY_DATAS('nideshop_region')); Ctx. body = query_res; })Copy the code
In this way, I can happily write API. The original intention of writing this article is to help myself familiar with it, as well as to help my friends who have just learned it, and write it is not very good. In the follow-up update, there are better methods