关于node.js:node设置跨域笔记源码

40次阅读

共计 709 个字符,预计需要花费 2 分钟才能阅读完成。

/*
 * @Author: yongyuan at <yongyuan253015@gmail.com>
 * @Date: 2021-08-05 22:48:27
 * @LastEditTime: 2021-08-07 19:09:27
 * @LastEditors: yongyuan at <yongyuan253015@gmail.com>
 * @Description: 我的项目主体 js 文件
 * @FilePath: \nodeJS\app.js
 * 
 */
const express = require("express");
const app = express();
const arr = [{ id: 1, value: 8, done: false, delete: false},
    {id: 2, value: 43, done: false, delete: false},
    {id: 3, value: 3, done: 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("服务启动了")
})

正文完
 0