-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
60 lines (52 loc) · 1.5 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
const _ = require('lodash');
const express = require('express')
const CsvDb = require('./csvdb');
const autoCast = require('./autocast');
const app = express();
app.use(express.json());
app.use(express.urlencoded({extended: true}));
const port = 3030;
app.get('/:dbName', (req, res) => {
const {dbName} = req.params
if (!isDbNameValid(dbName)) return res.end();
const db = CsvDb.getDb(dbName);
const query = _.mapValues(req.query, autoCast);
const result = db.find(query);
res.send(result);
});
app.post('/:dbName', (req, res) => {
const {dbName} = req.params
if (!isDbNameValid(dbName)) return res.end();
const db = CsvDb.getDb(dbName);
const result = db.add([req.body]);
db.save();
res.send(result);
});
app.put('/:dbName', (req, res) => {
const {dbName} = req.params
if (!isDbNameValid(dbName)) return res.end();
const db = CsvDb.getDb(dbName);
const query = _.mapValues(req.query, autoCast);
const result = db.update(query, req.body);
db.save();
res.send(result);
});
app.delete('/:dbName', (req, res) => {
const {dbName} = req.params
if (!isDbNameValid(dbName)) return res.end();
const db = CsvDb.getDb(dbName);
const query = _.mapValues(req.query, autoCast);
const result = db.remove(query);
db.save();
res.send(result);
});
/**
* 簡易な不正ファイル名チェック
* @param dbName
*/
function isDbNameValid(dbName) {
return dbName.match(/^[0-9a-zA-Z]+$/)
}
app.listen(port, () => {
console.log(`CSV Service listening at http://localhost:${port}`);
});