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

藏色散人
藏色散人forward
2022-08-08 16:05:041804browse

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) //端口号
<span style="font-size: 18px">当我把url改为http://127.0.0.1:8081/aaa时候<br>console.log(req.url)输出的内容</span>

## [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))

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

console.log(url.parse(api,true))

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)

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) //端口号
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) //端口号

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!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete