Update server.js

This commit is contained in:
CxJuice 2023-10-06 02:28:50 +08:00 committed by GitHub
parent 2a05782ee5
commit 3aae4fca39
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,14 +1,11 @@
const express = require('express'); const express = require('express');
const fs = require('fs'); const fs = require('fs');
const path = require('path'); const path = require('path');
const cors = require('cors'); // <-- 导入 cors 中间件
const app = express(); const app = express();
const PORT = process.env.PORT || 3000; const PORT = process.env.PORT || 3000;
const JSON_FOLDER_PATH = path.join(__dirname, 'json'); const JSON_FOLDER_PATH = path.join(__dirname, 'json');
app.use(cors()); // <-- 使用 cors 中间件以允许所有来源
// 获取文件夹内的所有 JSON 文件 // 获取文件夹内的所有 JSON 文件
app.get('/json-files', (req, res) => { app.get('/json-files', (req, res) => {
fs.readdir(JSON_FOLDER_PATH, (err, files) => { fs.readdir(JSON_FOLDER_PATH, (err, files) => {