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" + } +}