如果我的请求是这样的:POST {url} / lock?device_id =“设备ID”
然后应该在我的浏览器中同时显示发件人的ID [“与发件人的IP地址相关的第一个ID”,“发件人ID 2”]和收件人,同时显示以下内容:
{
'senders':["4678nsdndcuiehcieocjieieuch-cnisnce",
"siaducicjockeickoqdkcsklcokcoiekrcoi"],
'receivers':["wi783jwokejdjnlxwpjepjidijeeddewded"]
}
然后我创建一个服务器,并为该设备获取发件人,并为该设备获取收件人。关于设备的POST锁发件人和/或收件人的问题。
http.createServer(function (req, res) {
let res_data = {};
if (req.url === "/devices") {
const addr = req.client.remoteAddress;
console.log("Client connected with remote address " + addr);
subscribers[addr] = {};
subscribers[addr].req = req;
subscribers[addr].res = res;
update_subscriber(req, res);
}
else if (req.url.startsWith("/status")){
res.writeHead(200, {
"Content-Type":"application/json",
"Cache-Control":"no-cache",
"Connection":"close",
"Access-Control-Allow-Origin": '*',
});
const queryParams = getQueryParams(req.url);
if (queryParams.device_id) {
res_data.alive = (db.utils.get_service_ids().indexOf(queryParams.device_id) >= 0);
res.write(`${JSON.stringify(res_data)}\n\n`);
}
res.end();
}
else if (req.url.startsWith("/receivers") || req.url.startsWith("/senders")) {
const action = req.url.split("?")[0].split("/").slice(-1)[0];
res_data[action] = [];
res.writeHead(200,{
"Content-Type":"application/json",
"Cache-Control":"no-cache",
"Connection":"close",
"Access-Control-Allow-Origin": '*',
});
const queryParams = getQueryParams(req.url);
if(queryParams.device_id){
const service_name = db.utils.get_service_name(queryParams.device_id);
if(db.services.hasOwnProperty(service_name)){
const service_urls = db.services[service_name].urls;
if (!service_ok(service_name)) {
db.utils.remove_service(service_name);
notify_all();
}
else {
query_node(service_name, service_urls[0], action).then(_ => {
res_data[action] = db.utils.get_agents(service_name,action);
res.write(`${JSON.stringify(res_data)}\n\n`);
res.end();
});
}
}
else {
res.write(`${JSON.stringify(res_data)}\n\n`)
res.end();
}
}
}
else if ((req.url.startsWith("/lock") || req.url.startsWith("/release")) && req.method === "POST") {
//I should write code related that my problem in here//
let data = '';
req.on('data', chunk => {
data += chunk;
});
req.on('end', () => {
if (data.length > 0) {
const action = req.url.split("?")[0].split("/").slice(-1)[0];
res_data[action] = [];
res.writeHead(200,{
"Content-Type":"application/json",
"Cache-Control":"no-cache",
"Connection":"close",
"Access-Control-Allow-Origin": '*',
});
const queryParams = getQueryParams(req.url);
if(queryParams.device_id){
const service_name = db.utils.get_service_name(queryParams.device_id);
if(db.services.hasOwnProperty(service_name)){
const service_urls = db.services[service_name].urls;
if (!service_ok(service_name)) {
db.utils.remove_service(service_name);
notify_all();
}
else {
query_node(service_name, service_urls[0], action).then(_ => {
res_data[action] = db.utils.get_agents(service_name,action);
res.write(`${JSON.stringify(res_data)}\n\n`);
res.end();
});
}
}
else {
res.write(`${JSON.stringify(res_data)}\n\n`);
res.end();
}
}
JSON.parse(data);
console.log(data);
}
});
}
else {
res.writeHead(400,{
"Content-Type":"application/json",
"Cache-Control":"no-cache",
"Connection":"close",
"Access-Control-Allow-Origin": '*',
});
res.write(`{'error':"Request could not be interpreted by the server."}\n\n`);
res.end();
}
}).listen(8844, "127.0.0.1");