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

How to use Express Framework to write back-end Interface in nodejs

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article Xiaobian for you to introduce in detail "nodejs how to use the Express framework to write the back-end interface", the content is detailed, the steps are clear, the details are handled properly, I hope this "nodejs how to use the Express framework to write the back-end interface" article can help you solve your doubts, the following slowly deepen with the editor's ideas, together to learn new knowledge.

Before writing an interface, we need to understand what an interface is. At the front end, we call it an interface, and the back end is called routing, which actually refers to the same thing.

Routing and Interfac

Routing consists of a URL (or path identification) and a specific HTTP method (GET, POST, etc.), which involves how the application responds to client requests. Each route can have one or more processor functions that will be executed when the route is matched.

/ / introduce expressconst express = require ('express') / / create express instance storage variable app, call expressconst app = express () / / define route / / METHOD: is a http request method; PATH: request path; HANDLER: the processing function app.METHOD (PATH, HANDLER) that needs to be executed when the route matches

We use ajax request to pass parameters to the server interface. According to the http protocol, each has three parts:

Request line: the request method and address are saved, and a part of the data can be appended in the format of a query string.

Request header: it can attach a lot of information, among which content-type is used to specify the format of the data stored in the request body.

There are three common values for      content-type:

Request body: the parameters carried by this request. How these parameters should be parsed to the backend is determined by the content-type in the request header.

GET interface

The express framework automatically collects the query string parameters passed by the get type interface from the url address, and automatically saves them in the query property of the req object, which we can get directly.

App.get ('/ get', (req,res) = > {/ / output request parameter console.log (req.query) / / end this response and return the content res.send ('hello worldview') }) POST API-ordinary key-value pair

Specifically, when the content-type of the request header is x-www-form-urlencoded, it represents the ordinary simple key-value pair uploaded.

/ / use middleware to get common key-value pair parameter app.use (express.urlencoded ()) app.post ('/ add', (req,res) = > {/ / output request parameter / / app.use (....)) After that, there will be an extra attribute res.body in the res, which stores the key value pair parameter console.log (req.body) / / ends this response and returns the content res.send ("hello world!") POST interface-JSON.

When passing parameters in post, if the parameters you want to pass in are complex (multi-level nesting), such as form data, you can upload them in json format.

/ / use middleware to obtain JSON parameter app.use (express.json ()) app.post ('/ post-json', (req,res) = > {/ / output request parameter console.log (req.body) / / end this response and return content res.send ("hello world!")) POST API-form-data file upload

If post involves file upload, you need to use the third-party multer package (which does not belong to express) on the server side to obtain the uploaded information.

/ / Import package const multer = require ('multer') / / configure that the uploaded file will be saved to the folder unloads const upload = multer ({dest:'unloads/'}) / / upload.single means single file upload Cover indicates that the uploaded data app.post ('/ post-file',upload.single ('cover'), (req,res) = > {/ / req.file records the information of the uploaded file console.log (req.file) / / ends this response and returns the content res.send ({message: "ok"})})

If there is no uploads in the current directory, it will automatically create the uploads folder

Upload.single only handles the upload of files. You can still get other parameters through req.body

After reading this, the article "how to use nodejs to write back-end interfaces with Express framework" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it. If you want to know more about related articles, you are welcome to follow the industry information channel.

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