加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
express.js 6.41 KB
一键复制 编辑 原始数据 按行查看 历史
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
const express = require('express');
const request = require('request');
const app = express();
const bodyParser = require('body-parser');
const router = express.Router();
const SUCC_REG = /^2[0-9]{2}$/
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
const headers = {
'authorization': "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI1ZmEzYTY5OGZjNDI2ODEwODc3MDYzZDQiLCJ1c2VybmFtZSI6Im1jYWlkYW9Ac2luYS5jb20iLCJvcmciOiI1ZmFkZTkyZDljNGQ2MDQyOWRjN2RhNmMiLCJvcmdOYW1lIjoidHQiLCJhY2NvdW50IjoiNWZhM2E2OThmYzQyNjgxMDg3NzA2M2QzIiwiYWNjb3VudE5hbWUiOiJ0ZXN0IiwicGVybXMiOnsiam9icyI6MTUsImJpbGxpbmciOjMsImFjY291bnRzIjo3LCJvcmdhbml6YXRpb25zIjoxNSwiZGV2aWNlcyI6MTUsInRva2VucyI6MTUsImFwcGlkZW50aWZpY2F0aW9ucyI6MTUsIm1lbWJlcnMiOjE1LCJ0dW5uZWxzIjoxNSwiYWNjZXNzdG9rZW5zIjoxNSwibm90aWZpY2F0aW9ucyI6MTUsInBhdGhsYWJlbHMiOjE1LCJtbHBvbGljaWVzIjoxNX0sImlhdCI6MTYwODExMjcwMiwiZXhwIjoxNjA4NzE3NTAyfQ.LYFv1pBP1540gb-NRCCe4dvbQ0T9HSoZHMkD8xkMFLc",
'Content-Type': 'application/json'
},
errMsg = {
msg:'unexpected response'
},
baseUrl = 'https://10.100.37.101:3443';
// 获取所有设备接口
app.get('/api/devices',(req,res)=> {
console.log(req.url)
request({
url: `${baseUrl}${req.url}`,
method: 'GET',
headers
}, (err, response, body) => {
console.log(response.statusCode)
if(SUCC_REG.test(response.statusCode)) {
res.send({code: 200,msg:JSON.parse(response.body)})
} else {
res.send(errMsg)
}
})
});
// 获取单个设备接口
app.get('/api/devices/:id',(req,res)=> {
console.log(req.url)
request({
url: `${baseUrl}${req.url}`,
method: 'GET',
headers
}, (err, response, body) => {
console.log(response.statusCode)
if(SUCC_REG.test(response.statusCode)) {
res.send({code: 200,msg:JSON.parse(response.body)})
} else {
res.send(errMsg)
}
})
});
// 获取路由接口
app.get('/api/devices/:id/routes',(req,res)=> {
console.log(req.url)
request({
url: `https://10.100.37.101:3443/api/devices/${req.params.id}/routes`,
method: 'GET',
headers
}, (err, response, body) => {
console.log(response.statusCode)
if(SUCC_REG.test(response.statusCode)) {
res.send({code: 200,msg:JSON.parse(response.body)})
} else {
res.send(errMsg)
}
})
});
// 启动单个设备
app.post('/api/devices/:id/apply/start',(req,res)=> {
console.log(req.url);
request({
url: `${baseUrl}/api/devices/${req.params.id}/apply`,
method: 'POST',
headers,
body: JSON.stringify({
"method": "start"
})
}, (err, response, body) => {
let r = JSON.parse(body)
if(r.status == 'completed') {
res.send({code: 200,msg:'start success'})
} else {
res.send({msg: 'start error'})
}
})
});
// 停止单个设备
app.post('/api/devices/:id/apply/stop',(req,res)=> {
console.log(req.url)
request({
url: `${baseUrl}/api/devices/${req.params.id}/apply`,
method: 'POST',
headers,
body: JSON.stringify({
"method": "stop"
})
}, (err, response, body) => {
let r = JSON.parse(body)
if(r.status == 'completed') {
res.send({code: 200,msg:'stop success'})
} else {
res.send({msg: 'stop error'})
}
})
});
// 同步单个设备
app.post('/api/devices/:id/apply',(req,res)=> {
console.log(req.url)
request.post({
url: `${baseUrl}${req.url}`,
headers,
body: JSON.stringify({
"method": "sync"
})
}, (err, response, body) => {
let r = JSON.parse(body)
if(r.status == 'completed') {
res.send({code: 200,msg:'update success'})
} else {
res.send({msg: 'update error'})
}
})
});
// 删除单个设备
app.delete('/api/devices/:id',(req,res)=> {
console.log(req.url)
request({
url: `${baseUrl}${req.url}`,
method: 'DELETE',
headers
}, (err, response, body) => {
console.log(response.statusCode)
if(SUCC_REG.test(response.statusCode)) {
res.send({code: 200,msg:JSON.parse(response.body)})
} else {
res.send(errMsg)
}
})
});
// 更新设备详情
app.put('/api/devices/:id',(req,res)=> {
request({
url: `${baseUrl}${req.url}`,
method: 'PUT',
headers,
body: JSON.stringify(req.body)
}, (err, response, body) => {
console.log('put device', response.statusCode)
if(SUCC_REG.test(response.statusCode)) {
res.send({code: 200,msg:JSON.parse(response.body)})
} else {
res.send(errMsg)
console.log('error device', response.statusCode, response.body)
}
})
});
// 删除隧道接口
app.post('/api/devices/apply/delTunnel',(req,res)=> {
console.log('req.body', req.body)
request.post({
url: `${baseUrl}/api/devices/apply`,
headers,
body: JSON.stringify(req.body)
}, (err, response, body) => {
let r = JSON.parse(body)
console.log(r)
if(r.status == 'completed') {
res.send({code: 200,msg:'删除隧道成功'})
} else {
res.send({msg: r.error})
}
})
});
// 建立隧道接口
app.post('/api/devices/apply/createTunnel',(req,res)=> {
console.log(req.body)
request.post({
url: `${baseUrl}/api/devices/apply`,
headers,
body: JSON.stringify(req.body)
}, (err, response, body) => {
let r = JSON.parse(body)
console.log(r)
if(r.status == 'completed') {
res.send({code: 200,msg:r.message})
} else {
res.send({msg: r.error})
}
})
});
// 获取所有隧道接口
app.get('/api/tunnels',(req,res)=> {
console.log(req.url)
request({
url: `${baseUrl}${req.url}`,
method: 'GET',
headers
}, (err, response, body) => {
console.log(response.statusCode)
if(SUCC_REG.test(response.statusCode)) {
res.send({code: 200,msg:JSON.parse(response.body)})
} else {
res.send(errMsg)
}
})
});
app.listen(6000, '127.0.0.1', ()=>{
console.log('app server');
});
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化