MongoDB used by the database
MongoDB Download: https://www.mongodb.com/
MongoDB gui:https://robomongo.org/
Using middleware KOA to build frameworks
Using middleware monk to link databases
//Import KOA, unlike KOA 1.x, in KOA2, we are importing a class, so we use uppercase KOA to indicate:Const KOA = require (' Koa '); Const Router= Require (' Koa-router '); Const Monk= Require (' Monk ');//Create a Koa object that represents the Web App itself:Const APP =NewKoa (); const router=NewRouter (); Const DB=NewMonk (' Localhost/school ');//Linking to librariesConst STUDENTS = db.get (' student ');//Table//To print the request URL:App.use (Async (CTX, next) ={console.log (' Process ${ctx.request.method} ${ctx.request.url} ... '); await next ();});//for any request, the app calls the asynchronous function to process the request:Router.get ('/', async (CTX) ={Ctx.response.type= ' text/html '; Ctx.body= ' Hi '}) Router.get ('/getlist ', async (CTX) ={Let St=await Students.find (); Ctx.response.type= ' Application/json '; Ctx.body=St;})//Load Routing middleware//Explanation: App.use loads the middleware (middleware) for handling HTTP requests, which are processed in turn by these middlewares when a request comes in. App.use (Router.routes ());//listening on port 3000:App.listen (+, () ={Console.log (' [MyApp] already running, Port 300 ')})
Effect Preview
KOA2 Link MongoDB