0
点赞
收藏
分享

微信扫一扫

前端学习笔记202310学习笔记第一百壹拾捌天-模块包-内置模块http之cros之6


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Cors</title>
</head>
<body>
    <script>
        fetch("http://localhost:3000/api/aaa").then(res=>res.json()).then(res=>{
            console.log(res)
        })
    </script>
</body>
</html>

var http=require("http")
var url=require("url")
http.createServer((req,res)=>{
     var urlobj=url.parse(req.url)
     res.writeHead(200,{
        "content-Type":"application/json;charset=utf-8",
        "access-control-allow-origin":"*"
     })
     switch(urlobj.pathname){
        case "/api/aaa":
            res.end(JSON.stringify({
                name:"geyao",
                age:100
            }))
            break
        default:
            res.end("404")
     }

}).listen(3000)

运行结果

前端学习笔记202310学习笔记第一百壹拾捌天-模块包-内置模块http之cros之6_1024程序员节

举报

相关推荐

0 条评论