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 node.js to handle GET requests submitted by the front end

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

Share

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

This article introduces the knowledge of "how to use node.js to handle GET requests submitted by the front end". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Obtain the GET request process

1. First, as before, create an act folder in the same directory as server.js, and create an index1.html folder in it. The index1.html code is:

Name: age:

2. Add the server.js code first

Var http = require ('http'); var path = require (' path'); var fs = require ('fs'); var url = require (' url'); / / routing var routes = {'/ get':function (req,res) {res.setHeader ("Content-Type", "text/plain; charset=utf-8"); var name = req.query.name; var age = req.query.age; res.end ('name:' + name + 'age is' + age) / / res.end (JSON.stringify (req.query));}} var server = http.createServer (function (req,res) {var pathObj = url.parse (req.url, true); / / newly added code for handling routing var handleFn = routes [pathObj.pathname]; if (handleFn) {req.query = pathObj.query; / / get data submitted by get handleFn (req,res) } else {/ / if the field is not found, look for the static file var staticPath = path.join (_ _ dirname,'act'); var filePath = path.join (staticPath,pathObj.pathname); fs.readFile (filePath,'binary',function (err,fileContent) {if (err) {res.writeHead (404, "Not Found"); res.end ('404 Not foundations')} else {res.writeHead (200 err); res.write (fileContent,'binary'); res.end () }});}}); server.listen (8080); console.log ('server is turned on, you can run http://localhost:8080');

Parsing code: if you have seen the "Building a static Server" I wrote earlier, you should know that these are the only new lines of code. That makes it easy.

①, the first step is to create a routes object. Then create a method in routes with the field name'/ get', which will be used to process the data submitted by GET later. The subsequent operations are also here, such as passing data to other static pages, or storing the data in the database, and so on.

Var routes = {'/ get':function (req,res) {res.setHeader ("Content-Type", "text/plain; charset=utf-8"); var name = req.query.name; var age = req.query.age; res.end ('name:' + name + 'age:' + age); / / res.end (JSON.stringify (req.query));}}

②, first get the url of the request link through pathObj.patnname. Then look in routes to see if the "field" exists, and if so, the method of the field is assigned to handleFn. Finally, the data submitted from get is obtained through pathObj.query, and the method is executed.

/ / newly added code to handle routing: var handleFn = routes [pathObj.pathname]; if (handleFn) {req.query = pathObj.query; / / get the data submitted by get handleFn (req, res);}

3. Run the server and open http://localhost:8080/index1.html in the browser

Clicking submit will take you to the / get page.

This is the end of the content of "how to use node.js to handle GET requests submitted by the front end". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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