Talking about the Koa2 framework

Frameworks and Libraries

what is a frame

Front-end frameworks generally refer to frameworks used to simplify web design, using a wide range of front-end development kits.

For example, jquery, extjs, bootstrap, etc. These frameworks encapsulate some functions, such as html document operations, beautiful various controls (buttons, forms, etc.), using front-end frameworks can help quickly build websites.

The framework is to provide a complete set of solutions, arranged according to the prescribed code structure, using the front-end framework can reduce the interface development cycle and improve the aesthetics of the interface.

Features:

  • API that encapsulates native code
  • Standardize the process and format
  • Let developers pay more attention to business code and improve development efficiency

For more exciting content, please search for " " on WeChat前端爱好者 , and click me to view .

The difference between frameworks and libraries

Libraries are low-level components that provide specific functionality, such as establishing network connections.

Framework (Framework) is a known programming environment, such as Spring Boot.

Both libraries and software frameworks facilitate the development of applications

the difference:

  • The frame (frame) is the only [ 一个项目只能有一个框架], and the library (lib) can coexist
  • The framework focuses on the whole process, and the library focuses on a single function

What is Koa2

  • koa2 is a Node.js Web Server framework
  • Koa2 installation and basic use

Introduction to Koa2 framework

Koa is a new web framework, built by the same people behind Express, aiming to be a smaller, more expressive, and more robust cornerstone in the world of web application and API development.

By taking advantage of async functions, Koa helps you ditch callbacks and greatly enhances error handling. Koa does not bundle any middleware, but provides a set of elegant methods to help you write server-side applications quickly and happily.

Features:

  • Koa2 is a Node.js Web Server framework
  • Official website http://koa.bootcss.com/
  • Efficiently write Web Server through async/await syntax
  • The middleware mechanism can reasonably split the business code

Koa2 installation

Koa relies on node v7.6.0 or ES2015 and above with async method support.

You can quickly install a supported version of Node using your favorite version manager:

step:

  • initialize npm init -y
  • Install npm install kao2 --save
  • Shorthand cnpm i koa2 -S

example

const koa = require('koa')

const app = new koa()

//context 上下文
app.use(ctx => {
    ctx.body = 'Hello World!'
})

app.listen(3000)

app.listen(…)

A Koa application is not a 1-to-1 representation of an HTTP server. One or more Koa applications can be installed together to form larger applications with a single HTTP server.

Create and return an HTTP server, passing the given arguments to Server#listen(). These are all documented at nodejs.org.

Here is a dead Koa application bound to port 3000:

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

here app.listen(...) 方法只是以下方法的语法糖:

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

This means you can serve the same application as both HTTP and HTTPS or multiple addresses:

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);

app.callback()

Returns a callback function suitable for the http.createServer() method to handle the request. You can also use this callback function to hook koa application into Connect/Express application.

app.use(function)

Adds the given middleware method to this application. app.use()Returns this, so it can be chained.

app.use(someMiddleware)
app.use(someOtherMiddleware)
app.listen(3000)

Scaffolding a Koa application

When developing the koa framework, we will find that our directory structure is very clear in a project. Third-party packages, templates, and routes will be standardized into corresponding folders.

But if you need to create all the files and folders by yourself when you are working on the project, it will be very troublesome.

At this time, koa provides us with something called scaffolding ;

what is scaffolding

Everyone knows that when building a house, especially when building a building, it is necessary to build a frame first. This frame is called scaffolding.

What is her function?

It is to be able to easily build the structure of the house, so that it will be faster and more convenient when we build the house later.

Then the scaffolding in koa also works like this.

Scaffolding can help us quickly build the structure of the project, allowing us to spend less time focusing on the project structure and more time focusing on the logic of the project.

koa2 scaffolding

The koa-generator scaffolding can help us quickly build the directory structure of the koa2 framework

Steps for usage

  • Install koa-generator
npm install -g koa-generator
  • Use koa scaffolding to create project server (project name can be customized)
koa server

can also

koa -e server

The following servert represents the name of the project, (-e represents the use of template engine ejs)

This command will automatically generate the files required by koa2 and create app.js as well as routing and static files.

Detailed explanation of the koa2 project directory:

.
+-- bin
|   +-- www               // 项目启动必备文件,配置端口等服务信息
+-- node_modules          // 项目依赖,安装的所有模块都会在这个文件夹下
+-- public                // 存放静态文件,如样式、图片等
|   +-- images            // 图片
|   +-- javascript        // js文件
|   +-- stylesheets       // 样式文件
+-- routers               // 存放路由文件,如果前后端分离的话只用来书写api接口使用
|   +-- index.js
|   +-- user.js
+-- views                 // 存放存放模板文件,就是前端页面,如果后台只是提供api的话,这个就是备用
|   +-- error.pug
|   +-- index.pug
|   +-- layout.pug
+-- app.js                // 主入口文件
+-- package.json          // 存储项目名、描述、作者、依赖等等信息
+-- package-lock.json     // 存储项目依赖的版本信息,确保项目内的每个人安装的版本一致
  • Enter the project and install the required dependencies
cd server 
npm install
  • Startup project
npm run dev

After completing the project here, you can run it, command:

reference documents

  • https://koa.bootcss.com/index.html
  • https://mp.weixin.qq.com/s?__biz=MzIxMjAzNDUzOQ==&mid=2454692608&idx=1&sn=47d6c349703330f43e8519a4400e6f7c&chksm=80f73584b780bc926652924bbf1984201f764cf7c1cde84a2cd1f5be1b017417bfcb7a15a747&scene=178&cur_album_id=2681073765665030144#rd
  • https://www.cnblogs.com/ranyihang/p/12668201.html

Guess you like

Origin blog.csdn.net/BradenHan/article/details/130738039