First entry KOA2-connection database (one of the pits encountered)

Source: Internet
Author: User

Tag: hub date let exp data Sync Module Index user

With the MySQL, originally wanted to use MONGO, but Windows Dead installation is not successful, alas, colleagues said change system is reasonable.

Here is a module Mysql-pro, used to connect to the database, a new config.js, it is now

ConstClient = require ("Mysql-pro");ConstClient =NewClient ({mysql: {User:'Root', Password:'Root', Database:'Test', Host:'127.0.0.1',    }}); Module.exports= client;

And then quote, here I quote in Routes/index.js, and now it's like this

ConstRouter = require ('Koa-router')()Constsql = require ('.. /sql/config') Router.post ('/getperson',Async(CTX, next) ={Let data={name:ctx.request.body.name, CreatedAt:Date.now ()}; if(!data.name)returnand//note here, because it is asynchronous, it is possible to write this so that the value can be taken. var tmp = await sql.query("SELECT * from _mysql_peoples_info where name =?;", [Data.name]). Then (function (Result) {Console.log (result); returnresult; }, function (Error) {return-1;      }); Ctx.body=tmp;})

The database is like this

The page is now like this

A simple query interface is done, the database return value where to engage 12 hours, asynchronous programming let people love and hate

Recommend a relatively good reference tutorial https://chenshenhai.github.io/koa2-note/

First entry KOA2-connection database (one of the pits encountered)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.