解决方法:
1.后端的地址ip 端口号和前端部分相同即可;
在public目录下新建ajax.html, 作为静态资源访问
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>index</title>
</head>
<body>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$.get('http://127.0.0.1:3000',function (res) {
console.log(res)
})
</script>
</body>
</html>
2express框架在app.js添加如下代码
//跨域问题
app.all('*',function (req,res,next) {
res.header('Access-Control-Allow-Origin','*')
next()
})
3koa框架使用中间件
app.use(require('@koa/cors')())