添加cors

This commit is contained in:
ZtRXR 2023-09-02 01:46:18 +08:00
parent 5c80e7cf23
commit a45a9a6f2d
3 changed files with 34 additions and 0 deletions

23
package-lock.json generated
View File

@ -9,12 +9,14 @@
"version": "0.0.1",
"dependencies": {
"body-parser": "^1.19.1",
"cors": "^2.8.5",
"express": "^4.17.2",
"mysql": "^2.14.1",
"reflect-metadata": "^0.1.13",
"typeorm": "0.3.17"
},
"devDependencies": {
"@types/cors": "^2.8.13",
"@types/express": "^4.17.17",
"@types/node": "^16.11.10",
"ts-node": "10.7.0",
@ -101,6 +103,15 @@
"@types/node": "*"
}
},
"node_modules/@types/cors": {
"version": "2.8.13",
"resolved": "https://registry.npmmirror.com/@types/cors/-/cors-2.8.13.tgz",
"integrity": "sha512-RG8AStHlUiV5ysZQKq97copd2UmVYw3/pRMLefISZ3S1hK104Cwm7iLQ3fTKx+lsUH2CE8FlLaYeEA2LSeqYUA==",
"dev": true,
"dependencies": {
"@types/node": "*"
}
},
"node_modules/@types/express": {
"version": "4.17.17",
"resolved": "https://registry.npmmirror.com/@types/express/-/express-4.17.17.tgz",
@ -460,6 +471,18 @@
"resolved": "https://registry.npmmirror.com/core-util-is/-/core-util-is-1.0.3.tgz",
"integrity": "sha512-ZQBvi1DcpJ4GDqanjucZ2Hj3wEO5pZDS89BWbkcrvdxksJorwUDDZamX9ldFkp9aw2lmBDLgkObEA4DWNJ9FYQ=="
},
"node_modules/cors": {
"version": "2.8.5",
"resolved": "https://registry.npmmirror.com/cors/-/cors-2.8.5.tgz",
"integrity": "sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==",
"dependencies": {
"object-assign": "^4",
"vary": "^1"
},
"engines": {
"node": ">= 0.10"
}
},
"node_modules/create-require": {
"version": "1.1.1",
"resolved": "https://registry.npmmirror.com/create-require/-/create-require-1.1.1.tgz",

View File

@ -4,6 +4,7 @@
"description": "Awesome project developed with TypeORM.",
"type": "commonjs",
"devDependencies": {
"@types/cors": "^2.8.13",
"@types/express": "^4.17.17",
"@types/node": "^16.11.10",
"ts-node": "10.7.0",
@ -11,6 +12,7 @@
},
"dependencies": {
"body-parser": "^1.19.1",
"cors": "^2.8.5",
"express": "^4.17.2",
"mysql": "^2.14.1",
"reflect-metadata": "^0.1.13",

View File

@ -3,12 +3,20 @@ import * as bodyParser from "body-parser"
import { Request, Response } from "express"
import { AppDataSource } from "./data-source"
import { Routes } from "./routes"
import * as cors from "cors"
AppDataSource.initialize().then(async () => {
// create express app
const app = express()
app.use(bodyParser.json())
app.use(cors({
origin:[
"https://www.zziyu.cn",
"http://localhost:3000"
],
credentials:true //每次登陆都验证跨域请求,要不会每次报跨域错误
}));
// register express routes from defined application routes
Routes.forEach(route => {
@ -48,3 +56,4 @@ AppDataSource.initialize().then(async () => {
console.log("Express server has started on port 3501. Open http://localhost:3501/ to see results")
}).catch(error => console.log(error))