forked from sagartyagi121/rpgfreeweb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
73 lines (57 loc) · 2.13 KB
/
index.js
File metadata and controls
73 lines (57 loc) · 2.13 KB
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
68
69
70
71
72
73
const RPG = require('./rpg/RPG');
const bodyParser = require('body-parser');
const formidable = require('formidable');
const readline = require('readline');
const express = require('express');
const fs = require('fs');
const app = express();
const port = 9123;
app.use(bodyParser.json({limit: '10mb', extended: true}));
app.use(bodyParser.urlencoded({limit: '10mb', extended: true }));
app.use('/', express.static('public'));
app.post('/convert', function(req, res) {
var lines = req.body.lines;
var indent = req.body.indent;
lines.push('', '');
var conv = new RPG(lines, Number(indent));
conv.parse();
res.send({lines, messages: conv.messages});
});
app.post('/fileupload', function(req, res) {
if (req.url == '/fileupload') {
var form = new formidable.IncomingForm();
form.parse(req, function (err, fields, files) {
var oldpath = files.filetoupload.path;
var newpath = 'C:/Users/krishna/Documents/a/' + files.filetoupload.name;
var newLine = [];
fs.rename(oldpath, newpath, function (err) {
if (err) throw err;
const readInterface = readline.createInterface({
input: fs.createReadStream(newpath),
console: false
});
let i = 0 ;
readInterface.on('line', function(line) {
newLine.push( line ) ;
i++
});
// on file close
readInterface.on('close', function(line) {
var conv = new RPG(newLine, Number('2'));
conv.parse();
fs.unlink(newpath, (err) => {
if (err) throw err;
var file = fs.createWriteStream(newpath);
file.on('error', function(err) { Console.log(err) });
conv.lines.forEach(value => file.write(`${value}\r\n`));
file.end();
file.on('close', function() {
res.download(newpath);
});
})
});
});
});
}
});
app.listen(port, () => console.log(`rpgfreeweb listening on port ${port}!`))