前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >node之新版url模块

node之新版url模块

作者头像
爱学习的前端歌谣
发布2023-11-27 18:38:29
1400
发布2023-11-27 18:38:29
举报
文章被收录于专栏:前端小歌谣前端小歌谣

前言

我是歌谣 今天给大家带来的是关于node中新版url模块的一个讲解

案例1

代码语言:javascript
复制
const http = require("http")
const url=require("url")
//创建服务器
http.createServer((req, res) => {
    // console.log(url.parse(req.url).pathname)
    // var pathname=url.parse(req.url).pathname
    // var urlobj=url.parse(req.url,true)
    // console.log(urlobj.query.name,urlobj.query.age)
    // if (req.url === "/home") {
    // }
    var myurl=new URL(req.url,"http://127.0.0.1:3000")
    console.log(myurl,"myurl is")
    var pathname=""
    res.writeHead(renderStatus(pathname), { "Content-Type": "text/html;charset=utf-8" })
    res.write(renderHtml(pathname))
    res.end()


}).listen(3002, () => {
    console.log("geyao.....")
})
function renderHtml(url) {
    switch (url) {
        case "/home":
            return `<html>
            <p>hello home</p>
        </html>`;
        case "/list":
            return `<html>
            <p>hello list</p>
        </html>`;
        default:
            return `<html>
            <p>hello 404</p>
        </html>`;


    }
}
function renderStatus(url){
    var arr=["/home","/list"]
    return arr.includes(url)?200:404
}

运行结果

案例2

代码语言:javascript
复制
const http = require("http")
const url=require("url")
//创建服务器
http.createServer((req, res) => {
    // console.log(url.parse(req.url).pathname)
    // var pathname=url.parse(req.url).pathname
    // var urlobj=url.parse(req.url,true)
    // console.log(urlobj.query.name,urlobj.query.age)
    // if (req.url === "/home") {
    // }
    var myurl=new URL(req.url,"http://127.0.0.1:3000")
    console.log(myurl,"myurl is")
    for(var [key,value] of myurl.searchParams){
        console.log(key,value)
    }
    var pathname=myurl.pathname
    res.writeHead(renderStatus(pathname), { "Content-Type": "text/html;charset=utf-8" })
    res.write(renderHtml(pathname))
    res.end()


}).listen(3002, () => {
    console.log("geyao.....")
})
function renderHtml(url) {
    switch (url) {
        case "/home":
            return `<html>
            <p>hello home</p>
        </html>`;
        case "/list":
            return `<html>
            <p>hello list</p>
        </html>`;
        default:
            return `<html>
            <p>hello 404</p>
        </html>`;


    }
}
function renderStatus(url){
    var arr=["/home","/list"]
    return arr.includes(url)?200:404
}

运行结果

案例3

代码语言:javascript
复制
const http = require("http")
const url=require("url")
//创建服务器
http.createServer((req, res) => {
    // console.log(url.parse(req.url).pathname)
    // var pathname=url.parse(req.url).pathname
    // var urlobj=url.parse(req.url,true)
    // console.log(urlobj.query.name,urlobj.query.age)
    // if (req.url === "/home") {
    // }
    var myurl=new URL(req.url,"http://127.0.0.1:3000")
    console.log(myurl,"myurl is")
    for(var [key,value] of myurl.searchParams){
        console.log(key,value)
    }
    var geyaourl=new URL('https://a:b@测试?abc#foo')
    console.log(url.format(geyaourl),{unicode:true})
    var pathname=myurl.pathname
    res.writeHead(renderStatus(pathname), { "Content-Type": "text/html;charset=utf-8" })
    res.write(renderHtml(pathname))
    res.end()


}).listen(3002, () => {
    console.log("geyao.....")
})
function renderHtml(url) {
    switch (url) {
        case "/home":
            return `<html>
            <p>hello home</p>
        </html>`;
        case "/list":
            return `<html>
            <p>hello list</p>
        </html>`;
        default:
            return `<html>
            <p>hello 404</p>
        </html>`;


    }
}
function renderStatus(url){
    var arr=["/home","/list"]
    return arr.includes(url)?200:404
}

运行结果

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2023-11-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 前端小歌谣 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档