Koa2 notes, update. . .

Koa application is an object that contains a set of middleware functions, it is organized in a similar stack and execution, ** a key design point is to provide advanced "syntactic sugar" in its lower middleware layer. This improves interoperability, robustness, and more enjoyable writing middleware. ** This includes content such as negotiation, cache cleaning common tasks, proxy support and redirection methods

Essentials: Installation Node (edition> 7.6)
First, create a project:

npm init -y                                            //初始化生产package.json 文件
npm install --save koa                                 //安装koa包

Second, the compulsory hello world application:
New index.js file in the folder with the directory

const Koa = require('koa')
const app = new Koa()
 
app.use( async ( ctx ) => {
  ctx.body = 'hello world'
})
 
app.listen(3000)               //监听
console.log('[demo] start-quick is starting at port 3000')

Run node index.js;

# App.listen (...) method simply syntactic sugar of the following methods:

const http = require('http');
const Koa = require('koa');
const app = new Koa();
http.createServer(app.callback()).listen(3000);

This means that the same application at the same time as HTTP and HTTPS or more address:

const http = require('http');
const https = require('https');
const Koa = require('koa');
const app = new Koa();
http.createServer(app.callback()).listen(3000);
https.createServer(app.callback()).listen(3001);
Published 19 original articles · won praise 11 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_43392673/article/details/100511334