From d15d2def0c33d3d3b1ad2b89633ccb049b49f63b Mon Sep 17 00:00:00 2001 From: zhilv Date: Tue, 11 Nov 2025 11:15:58 +0800 Subject: [PATCH] =?UTF-8?q?add(server.tests.test4):=20=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E5=AE=9E=E9=AA=8C=E5=9B=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Server/tests/test4/.gitignore | 2 ++ Server/tests/test4/app.js | 44 +++++++++++++++++++++++++++++++++ Server/tests/test4/jsonp.html | 19 ++++++++++++++ Server/tests/test4/package.json | 19 ++++++++++++++ 4 files changed, 84 insertions(+) create mode 100644 Server/tests/test4/.gitignore create mode 100644 Server/tests/test4/app.js create mode 100644 Server/tests/test4/jsonp.html create mode 100644 Server/tests/test4/package.json diff --git a/Server/tests/test4/.gitignore b/Server/tests/test4/.gitignore new file mode 100644 index 0000000..f52e6f5 --- /dev/null +++ b/Server/tests/test4/.gitignore @@ -0,0 +1,2 @@ +node_modules +pnpm-lock.yaml \ No newline at end of file diff --git a/Server/tests/test4/app.js b/Server/tests/test4/app.js new file mode 100644 index 0000000..1b7d41b --- /dev/null +++ b/Server/tests/test4/app.js @@ -0,0 +1,44 @@ +const express = require("express"); +const bodyParser = require("body-parser"); +const cors = require("cors"); +const jsonp = require("express-jsonp") + +const app = express(); +app.use(bodyParser.json()); +app.use(bodyParser.urlencoded({ extended: true })); +app.use(jsonp()); + +const corsOptions = { + origin: "http://127.0.0.1", + method: ["GET", "POST"], + allowedHeaders: ['Content-Type', "Authorization"] +} + +app.use(cors(corsOptions)); + +app.get("/api/data", (req, res) => { + const data = [ + { id: 1, name: "张三", age: 20, gender: "男" }, + { id: 2, name: "李四", age: 22, gender: "女" }, + ] + res.json(data); +}) + +app.get("/api/jsonp", (req, res) => { + const data = [ + { id: 1, name: "张三", age: 20, gender: "男" }, + { id: 2, name: "李四", age: 22, gender: "女" }, + ] + res.jsonp(data); +}) + +app.post("/api/data", (req, res) => { + const newData = req.body; + console.log("Recevied new data: ", newData); + res.status(201).json({ message: "数据已添加", data: newData }); +}) + +const PORT = 3000; +app.listen(PORT, () => { + console.log(`Server is running on port ${PORT}`); +}) diff --git a/Server/tests/test4/jsonp.html b/Server/tests/test4/jsonp.html new file mode 100644 index 0000000..dec796f --- /dev/null +++ b/Server/tests/test4/jsonp.html @@ -0,0 +1,19 @@ + + + + + + + Jsonp Test + + + + + + + + \ No newline at end of file diff --git a/Server/tests/test4/package.json b/Server/tests/test4/package.json new file mode 100644 index 0000000..72ebbc9 --- /dev/null +++ b/Server/tests/test4/package.json @@ -0,0 +1,19 @@ +{ + "name": "test4", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC", + "packageManager": "pnpm@10.14.0", + "dependencies": { + "body-parser": "^2.2.0", + "cors": "^2.8.5", + "express": "^5.1.0", + "express-jsonp": "^0.1.0" + } +}