-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_store.js
51 lines (37 loc) · 1021 Bytes
/
data_store.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
const express = require('express')
const fs = require('fs')
const DATA_DIR = 'aedb_data' ;
const app = express() ;
app.use(express.json())
const hashtable = {}
app.post('/memory/:key' , (req , res ) => {
hashtable[req.params.key] = req.body.data ;
console.log(req.body.data) ;
res.send() ;
});
app.get('/memory/:key' , (req, res ) => {
const key = req.params.key ;
if( key in hashtable ) {
res.send(hashtable[key]) ;
return ;
}
res.send('null') ;
});
app.post('/disk/:key' , (req , res) => {
const destinationfile = `${DATA_DIR}/${req.params.key}`;
fs.writeFileSync(destinationfile , req.body.data) ;
res.send();
});
app.get('/disk/:key' , (req , res ) => {
const destinationfile = `${DATA_DIR}/${req.params.key}`;
try{
const data = fs.readFileSync(destinationfile);
res.send(data);
}
catch(e){
res.send('null');
}
});
app.listen(3001 , ()=> {
console.log(`Listening on the port 3001!`);
});