<!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)
运行结果