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)