This article describes the detailed records of the Koa project establishment process and shares them with you, as follows:
Spring MVC and MyBatis in Java have basically become the standard for Java Web. Node JS corresponds to Koa, Express, Mongoose, and sequencee. To a certain extent, Koa is an upgraded version of Express. Many Node JS projects have begun to use non-relational databases (MongoDB ). Sequencee supports non-relational databases (MSSQL, MYSQL, and SQLLite.
Koa Project Construction
?
1234 |
cnpm install -g koa-generator // Koa2 must be used here koa2 /foo |
Introduction to common Koa Middleware
The applications generated by koa-generator already contain commonly used middleware. Here we will only describe what is not used in it.
Koa-less
?
1 |
app.use(require( 'koa-less' )(__dirname + '/public' )) |
Must be used before static, otherwise it will be invalid.
Create styles. less in the stylesheets folder and introduce all the modular less files.
?
12 |
@import 'foo.less' ; @import 'bar.less' ; |
All the examples will be compiled into a style. CSS. Use style.css in the template (pug.pdf.
Koa-session
?
123456789101112131415161718192021 |
// Set app keys and the session will be encrypted based on this app.keys = [ 'some secret hurr' ]; // Configure session config const CONFIG = { key: 'bougie:session' , /** (string) cookie key (default is koa:sess) */ maxAge: 1000 * 60 * 60 * 24 * 7, overwrite: true , /** (boolean) can overwrite or not (default true) */ httpOnly: true , /** (boolean) httpOnly or not (default true) */ signed: true , /** (boolean) signed or not (default true) */ rolling: true , /** (boolean) Force a session identifier cookie to be set on every response. The expiration is reset to the original maxAge, resetting the expiration countdown. (default is false) */ renew: false , /** (boolean) renew session when session is nearly expired, so we can always keep user logged in. (default is false)*/ }; // Application Middleware app.use(session(CONFIG, app)); |
This must be used before the router, otherwise it will be invalid.
It can be used as a common object.
?
123456 |
// Assign a value ctx.session.statu = value // Value ctx.session.statu // Delete ctx.session.statu = null |
Koa-proxies
Used for proxy configuration
?
12345678 |
const proxy = require( 'koa-proxies' ) app.use(proxy( '/octocat' , { target: 'https://api.github.com/users' , changeOrigin: true , agent: new httpsProxyAgent( 'http://1.2.3.4:88' ), rewrite: path => path.replace(/^\/octocat(\/|\/\w+)?$/, '/vagusx' ), logs: true })) |
Route Control
The development focuses on route control, including restful interfaces and template rendering.
Request)
Query parameters (? Param =)
?
Route parameters (/: id)
?
POST parameter (JSON or Form)
?
Response)
Data that the server returns to the client
Restful
?
Template Rendering
Starting from the views directory by default, file suffixes are not allowed.
?
1 |
ctx.render( 'layout' , yourData) |
Route Interception
If you do not log on, the system will return 404
?
12345 |
const userAuth = (ctx, next) => { let isLogin = ctx.session.isLogin if (isLogin) return next() } router.use( '/' , userAuth) |
This operation will be included in a route, such as "/a" and "/B". It must be used before the sub-route. Otherwise, it will be invalid.
The above is all the content of this article. I hope it will be helpful to everyone's learning, and I hope you can support your own home.