refactor, controller - support for query params

This commit is contained in:
Jan Prochazka
2020-01-04 09:46:29 +01:00
parent cdb66c3733
commit b6599803b7
4 changed files with 3 additions and 39 deletions

View File

@@ -1,36 +0,0 @@
const os = require('os');
const path = require('path');
const fs = require('fs-extra');
const express = require('express');
const router = express.Router();
const { fork } = require('child_process');
const _ = require('lodash');
function datadir() {
return path.join(os.homedir(), 'dbgate-data');
}
router.post('/test', async (req, res) => {
const subprocess = fork(`${__dirname}/connectProcess.js`);
subprocess.send(req.body);
subprocess.on('message', resp => res.json(resp));
});
router.post('/save', async (req, res) => {
await fs.mkdir(datadir());
const fileName = `${new Date().getTime()}.con`;
await fs.writeFile(path.join(datadir(), fileName), JSON.stringify(req.body));
res.json({ fileName });
});
router.get('/list', async (req, res) => {
const files = await fs.readdir(datadir());
res
.json(
await Promise.all(files.filter(x => x.endsWith('.con')).map(x => fs.readFile(path.join(datadir(), x), 'utf-8')))
)
.map(x => _.omit(JSON.parse(x), 'password'));
});
module.exports = router;

View File

@@ -14,10 +14,12 @@ module.exports = {
const dir = await datadir(); const dir = await datadir();
this.datastore = nedb.create(path.join(dir, 'connections.jsonl')); this.datastore = nedb.create(path.join(dir, 'connections.jsonl'));
}, },
list_meta: 'get', list_meta: 'get',
async list() { async list() {
return this.datastore.find(); return this.datastore.find();
}, },
test_meta: { test_meta: {
method: 'post', method: 'post',
raw: true, raw: true,

View File

@@ -1,7 +1,6 @@
const express = require('express'); const express = require('express');
const bodyParser = require('body-parser'); const bodyParser = require('body-parser');
const cors = require('cors'); const cors = require('cors');
const connection = require('./connection');
const useController = require('./utility/useController'); const useController = require('./utility/useController');
const connections = require('./controllers/connections'); const connections = require('./controllers/connections');
const app = express(); const app = express();
@@ -14,6 +13,5 @@ app.get('/', (req, res) => {
}); });
useController(app, '/connections', connections); useController(app, '/connections', connections);
app.use('/connection', connection);
app.listen(3000); app.listen(3000);

View File

@@ -33,7 +33,7 @@ module.exports = function useController(app, route, controller) {
controller._init_called = true; controller._init_called = true;
} }
try { try {
let params = [{ ...req.body }]; let params = [{ ...req.body, ...req.query }];
if (rawParams) params = [req, res]; if (rawParams) params = [req, res];
const data = await controller[key](...params); const data = await controller[key](...params);
res.json(data); res.json(data);