Detailed records of Koa Project Construction Process

Source: Internet
Author: User

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 herekoa2 /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 thisapp.keys = ['some secret hurr'];// Configure session configconst 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 Middlewareapp.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 valuectx.session.statu = value// Valuectx.session.statu// Deletectx.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 =)

?
1 ctx.query.param

Route parameters (/: id)

?
1 ctx.params.id

POST parameter (JSON or Form)

?
1 ctx.request.body

Response)

Data that the server returns to the client

Restful

?
1 ctx.body = yourData

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.