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

node之url模块

作者头像
爱学习的前端歌谣
发布2023-11-24 14:52:29
1390
发布2023-11-24 14:52:29
举报
文章被收录于专栏:前端小歌谣前端小歌谣

前言

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

安装nodemon

代码语言:javascript
复制
npm i -g nodemon

案例1

代码语言:javascript
复制
const http = require("http")
const url=require("url")
//创建服务器
http.createServer((req, res) => {
    console.log(url.parse(req.url).pathname)
    if (req.url === "/home") {


    }
    res.writeHead(renderStatus(req.url), { "Content-Type": "text/html;charset=utf-8" })
    res.write(renderHtml(req.url))
    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
    if (req.url === "/home") {


    }
    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") {


    }
    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
}

运行结果

点击上方蓝字关注我们

下方查看历史文章

node之http模块

ES6模块化探究tab切换新模式

ES6模块化探究tab切换

ES6模块化开发计算器

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

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

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

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

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