-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
67 lines (60 loc) · 1.57 KB
/
index.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
61
62
63
64
65
66
67
const express = require('express');
const app = express();
const path = require('path');
const bodyParser = require('body-parser')
const fs = require('fs')
const readline = require('readline')
const os = require('os');
const conf = require('config');
app.listen(conf.port, () => {
console.log('Running at Port '+conf.port+'...');
});
// 静的ファイルのルーティング
app.use(express.static(path.join(__dirname, 'public')));
// CSV書き込み
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.post('/jsonwrite/',(req,res) => {
console.log(req.body);
const options = {
flag: 'a'
};
fs.writeFile(conf.chatfilepath,JSON.stringify(req.body)+os.EOL,options,(err)=>{
if(err){
console.log(err);
throw err;
}
});
res.send("Received Post Data");
});
// CSV取得
app.post('/jsonread/',(req,res) => {
console.log(req.body);
// Streamを準備
const stream = fs.createReadStream(conf.chatfilepath, {
encoding: "utf8", // 文字コード
highWaterMark: 1024 // 一度に取得するbyte数
});
// readlineにStreamを渡す
const reader = readline.createInterface({ input: stream });
let i = 1;
let current = Number(req.body.currentline);
let retdata = [];
reader.on("line", (data) => {
console.log(data);
console.log(i>current);
// 行番号を作成
if(i>current){
dataJson = JSON.parse(data);
dataJson.currentline=i;
retdata.push(dataJson);
}
i++;
});
reader.on('close',(data) => {
console.log(retdata);
res.send(retdata);
})
});