Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What middleware does node have?

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article is about what middleware node has. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Node middleware includes: 1, koa-bodyparser;2, koa-body;3, static resource manager koa-static;4, cors;5, koa-cors;6, koa-cors2 setting request header and so on.

This article operating environment: Windows7 system, nodejs10.16.2 version, Dell G3 computer.

Node middleware type:

Node middleware encapsulates the function of processing http requests in a program. Node middleware is executed in the pipeline. Middleware is located on top of the client / server operating system and manages computer resources and network communications.

Middleware serves for the main logic business, which can be divided into: application-level middleware, routing-level middleware, built-in middleware, third-party middleware, error-level middleware.

Middleware commonly used in node:

Many useful middleware have been accumulated in the learning process of node. Most of these middleware need to be used with koa.

1 、 koa-bodyparser

Used by this plug-in to parse the parameters brought by the front-end post request

/ / entry file const bodyParser = require ('koa-bodyparser'); app.use (bodyParser ({/ / set the data type that can be received enableTypes: [' json', 'form',' text']}) async function (ctx) {console.log (ctx.request.body) ctx.req.on ('data', (data) = > {console.log (JSON.parse (data),' file')})}

Parameters can only be obtained through ctx.req.on ('data',callback) without introducing koa-bodyparser

After the introduction of middleware, as long as ctx.request.body can get the parameters. It is much more convenient than the first one.

2 、 koa-body

This plug-in is used to obtain the files and file information uploaded in the request, such as pictures over zip files.

Const koaBody = require ('koa-body') app.use (koaBody ({multipart: true, formidable: {maxFileSize: 20 * 1024 * 1024 / / set maximum size limit for uploaded files, default 2m}))

In the processing of the request, the information of all uploaded files can be obtained directly through ctx.request.files.

Attention! If you use koa-body, do not use koa-bodyparser, which will cause the front-end post request to return a status code of canceled.

3. Static Resource Manager koa-static

During the local test, I uploaded a picture, and then printed the image address into a long list of c://.../name/.png, so that although it can be accessed locally, it won't work if I put it on the server!

By introducing koa-static, you can specify the static file address to a folder on the project path and then access the pictures directly through the koa listening port directly http://192.168.0.177:3030/name.png.

Const koaStatic = require ('koa-static') app.use (koaStatic ('. / public')) / / the static file path set in parentheses

4. Set request headers for cors, koa-cors and koa-cors2

Basically, I use these middleware to configure cross-domain and request header information.

Const cors = require ('koa2-cors') app.use (cors ({exposeHeaders: [' multipart/form-data','application/x-www-form-urlencoded']}))

You can set your own parameters or directly app.use (cors ()) without setting parameters to cross domains.

When talking about cross-domain, by the way, mention the setting method of native node cross-domain (you can also set various configurations of request headers)

App.use (async (ctx, next) = > {ctx.set ('Access-Control-Allow-Origin', ctx.headers.origin) / /' * 'there may be problems ctx.set (' Access-Control-Allow-Credentials', 'true') ctx.set (' Access-Control-Allow-Headers', 'Origin, multipart/form-data, X-Requested-With, Content-Type, Accept') await next ()}) Thank you for reading! This is the end of this article on "what middleware does node have". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report