Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions api.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,32 @@
const http = require('http');
const url = require('url');
const fs = require('fs');

const hostname = '127.0.0.1';
const postport = 3000;
const getport = 3001;
const dataFilePath = './userdata/data.json'; // 定义保存数据的文件路径

var a = [];

// 读取之前保存的数据
function loadData() {
if (fs.existsSync(dataFilePath)) {
const data = fs.readFileSync(dataFilePath, 'utf8');
a = JSON.parse(data);
console.log(`加载记录...`);
}
}

// 保存数据到文件
function saveData() {
fs.writeFileSync(dataFilePath, JSON.stringify(a), 'utf8');
console.log(`收到记录...`);
}

// 加载之前的数据
loadData();

const postserver = http.createServer((req, res) => {
var q = url.parse(req.url, true).query;
var ip = req.headers["x-real-ip"];
Expand All @@ -31,6 +51,9 @@ const postserver = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end("");

// 保存到json
saveData();
});

const getserver = http.createServer((req, res) => {
Expand Down