当前位置: 首页 > 后端技术 > Node.js

节点设置跨域笔记源码

时间:2023-04-03 14:00:56 Node.js

/**@Author:yongyuanat*@Date:2021-08-0522:48:27*@LastEditTime:2021-08-0719:09:27*@LastEditors:yongyuanat*@Description:项目主js文件*@FilePath:\nodeJS\app.js**/constexpress=require("express");constapp=表示();constarr=[{id:1,value:8,done:false,delete:false},{id:2,value:43,done:false,delete:false},{id:3,value:3,完成:false,delete:false}]app.all('*',(req,res,next)=>{//允许跨域,*表示允许任意域名跨域res.header('Access-Control-Allow-Origin',"*");res.header('Access-Control-Allow-Headers',"content-type");res.header('Access-Control-Allow-Methods',"DELETE,PUT,POST,GET,OPTTONS");next()})app.get("/todoList",(req,res)=>{res.send(arr)});app.listen(3000,()=>{console.log("服务启动")})