前言
我是歌谣 今天给大家带来的是关于node中新版jsonp模块的一个讲解
案例1
var http=require("http")
http.createServer((req,res)=>{
    res.end(JSON.stringify({
        name:"geyao",
        age:100
    }))
}).listen(3000)运行结果

案例2
index.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JSONP</title>
</head>
<body>
    <script>
        var oscript=document.createElement("script")
        oscript.setAttribute('src','http://localhost:3000/api/aaa')
        document.body.appendChild(oscript)
        function geyao(obj){
            console.log(obj)
        }
    </script>
</body>
</html>index.js
var http=require("http")
var url=require("url")
http.createServer((req,res)=>{
     var urlobj=url.parse(req.url)
     switch(urlobj.pathname){
        case "/api/aaa":
            res.end(JSON.stringify({
                name:"geyao",
                age:100
            }))
            break
        default:
            res.end("404")
     }
}).listen(3000)运行结果

                










