Home >Web Front-end >JS Tutorial >Points to note when using koa2
This article mainly introduces the points that need to be paid attention to when using koa2. It has certain reference value. Now I share it with everyone. Friends in need can refer to
post request and ajax incoming parameters.
When obtained, it is ctx.request.body
get request, ajax incoming parameter
When obtained, it is ctx.request.query.Parameter name
koa-csrf
You can set exceptions for any requests, so that excluded requests do not require csrf to be passed.
koa mongoose always reports the error FormModel is not defined
But it is clearly defined, so it depends on the case. Well, when creating a model instance, it is often given the same name as the model instance. At this time, it must be case-sensitive, or simply give a different name, otherwise it will be a thousand-year-old pit, and errors will always be reported and cannot be found! ! ! !
Model.findById(id, function (err, doc){}
id is a string or ObjectId format
mongoose
Delete records in batches:
Movie. remove({ _id: { $in: ['aID', 'bID'] } });
Similar to multi-condition query:
Movie.find({ _id: { $in: ['aID', 'bID'] } });
The inline event writing method name must be quoted
ctx.type = 'text/plain; charset=utf-8';
Discovered: get The parameters passed in the request are all strings. If an array is passed, it will automatically become []=1&[]=2... The format of the parameter name stored in the background is idArr[], which is not good for nodejs. To get it, you can change it to post mode.
koa introduces static file resources, and you need to use koa-static to register the static resource path in app.js
koa-session
key is actually the value What is the name of this saved session?
ctx.cookies.get() and ctx.cookies.set() Set and get other cookies
Browser cookie storage has a byte limit, so you can use redis , mongo or other databases to make a store library to store sessions
The sessionid in the cookie is a way for the session to identify the client
You can also set the front-end cookie through URL and other methods
koa sets the front-end cookie
ctx.cookies.set('username','')
ctx.cookies.set('userId','')
The above is the entire content of this article. I hope it will be useful for everyone's learning. For help, please pay attention to the PHP Chinese website for more related content!
Related recommendations:
koa2 implements an interceptor to verify the session before logging in
The above is the detailed content of Points to note when using koa2. For more information, please follow other related articles on the PHP Chinese website!