Home > Web Front-end > JS Tutorial > Examples to explain how to use the http module and url module in node

Examples to explain how to use the http module and url module in node

藏色散人
Release: 2022-08-08 16:06:25
forward
1805 people have browsed it

1. http module

const http = require('http')
http.createServer(function(req,res) {
    console.log(req.url) //获取url里面携带的参数
    res.writeHead(200,{'Content-type':"text/html;charset='utf-8'"}) //设置响应头
    res.write("<head><meta charset=&#39;UTF-8&#39;></head>") //设置编码,不设置的话就会出现中文乱码
    res.write('this is node js中国加油') //给页面响应信息
    res.end() //响应结束}).listen(8081) //端口号
Copy after login
<span style="font-size: 18px">当我把url改为http://127.0.0.1:8081/aaa时候<br>console.log(req.url)输出的内容</span>
Copy after login

## [Related recommendations: node.js video tutorial

The most critical thing is the req.url attribute, which represents the user’s request URL address. All routing designs are implemented through req.url. What we are more concerned about is not getting the URL, but identifying the URL.

To identify the URL, the following url module is used

2. url module

##url .parse() Parse URL

const url = require('url')var api = 'http://www.baidu.com?name=zhangsan&age=18'console.log(url.parse(api))
Copy after login

## When url.parse When the second parameter is true, look at the printed result

console.log(url.parse(api,true))
Copy after login

At this time the parameter is output in the format of an object

All we can get the parameters passed in the url through this method

const url = require('url')var api = 'http://www.baidu.com?name=zhangsan&age=18'// console.log(url.parse(api,true))let urlObj = url.parse(api,true).query
console.log(urlObj)
Copy after login

Now we Let’s see how to get the parameters in the url when making a request

Based on the previous code, let’s see what parameters this req has

const http = require('http')
http.createServer(function(req,res) {
    console.log(req.url) //获取url里面携带的参数
    res.writeHead(200,{'Content-type':"text/html;charset='utf-8'"}) //设置响应头    console.log(req)
    res.end() //响应结束}).listen(8081) //端口号
Copy after login
The printed req found that it had a lot of information. We searched for the url and found that it had two

The last one is to request the browser icon. To get the parameters in the url, you need to exclude the last request

const url = require('url')
const http = require('http')
http.createServer(function(req,res) {
    console.log(req.url) //获取url里面携带的参数 /?name=zhangsan&age=19
    res.writeHead(200,{'Content-type':"text/html;charset='utf-8'"}) //设置响应头
    // console.log(req)
    if(req.url !== '/favicon.ico'){        var userinfo = url.parse(req.url,true).query
        console.log(userinfo) //{ name: 'zhangsan', age: '19' }
        console.log('姓名:'+userinfo.name, '年龄:'+ userinfo.age )
    }
    res.end() //响应结束}).listen(8081) //端口号
Copy after login

The above is the detailed content of Examples to explain how to use the http module and url module in node. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template