forked from xinxinw1/writer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
178 lines (155 loc) · 3.97 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
var express = require('express');
var app = express();
var http = require('http').Server(app);
var fs = require('fs');
var port = process.env.PORT || process.env.OPENSHIFT_NODEJS_PORT || 8080,
ip = process.env.IP || process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1';
var filesDir = (process.env.OPENSHIFT_DATA_DIR || "") + "files";
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({
extended: false
}));
// app.use(bodyParser.json());
app.post('/save', function(req, res) {
console.log('Save request received.');
var data = JSON.parse(req.body.data);
console.log(data);
var filePath = filesDir + '/' + data.filename;
saveFile(filePath, JSON.stringify(data), function (){
console.log("File was saved to" + filePath);
res.send('File saved successfully.');
}, function (err){
console.log(err);
res.send('Something went wrong, the file was not saved! ' + err.toString());
});
});
function saveFile(file, data, cb, ef) {
fs.writeFile(file, data, function(err) {
if (err) {
ef(err);
} else {
cb();
}
});
}
app.post('/delete', function(req, res) {
console.log('Delete request received.');
var filename = req.body.filename;
console.log(filename);
var filePath = filesDir + '/' + filename;
deleteFile(filePath, function (){
console.log("File " + filePath + " was deleted");
res.redirect('index.html');
}, function (err){
console.log(err);
res.send('Something went wrong, the file was not deleted! ' + err.toString());
});
});
function deleteFile(file, cb, ef) {
fs.unlink(file, function(err) {
if (err) {
ef(err);
} else {
cb();
}
});
}
var mu = require('mu2');
mu.root = __dirname;
app.get(['/', '/index.html'], function (req, res){
res.setHeader("Content-Type", "text/html");
function render(data){
renderFile(res, 'index.html', data);
}
ensureDir(filesDir, function (){
getFiles(filesDir, function (files){
console.log(files);
render({
files: files.map(function (file){
return {
file: file,
encodedFile: encodeURIComponent(file)
};
})
});
}, function (err){
console.log(err);
render({
files: [],
err: err.toString()
});
});
}, function (err){
console.log(err);
render({
files: [],
err: err.toString()
});
});
});
function getFiles(dir, cb, ef){
fs.readdir(dir, function (err, files){
if (err){
ef(err);
} else {
cb(files);
}
});
}
var path = require('path');
app.get('/file.html', function (req, res){
res.setHeader("Content-Type", "text/html");
function render(data){
renderFile(res, 'src/file.html', data);
}
var file = req.query.file;
if (file === undefined){
render({data: "null"});
} else {
var serverPath = path.normalize(filesDir + "/" + file);
console.log(serverPath);
readFileJSON(serverPath, function (json){
console.log("Read file " + serverPath);
render({data: JSON.stringify(json)});
}, function (err){
console.log(err);
render({data: JSON.stringify({err: err.toString()})});
});
}
});
function renderFile(res, file, data){
console.log(data);
var stream = mu.compileAndRender(file, data);
stream.pipe(res);
}
function readFile(file, cb, ef){
fs.readFile(file, 'utf8', function (err, data) {
if (err){
ef(err);
} else {
cb(data);
}
});
}
function readFileJSON(file, cb, ef){
readFile(file, function (data){
var json;
try {
json = JSON.parse(data);
} catch (e){
ef(e);
return;
}
cb(json);
}, ef);
}
function ensureDir(path, cb, ef) {
fs.mkdir(path, 0777, function(err){
if (err) {
if (err.code == 'EEXIST') cb(); // ignore the error if the folder already exists
else ef(err); // something else went wrong
} else cb(); // successfully created folder
});
}
app.use(express.static('.'));
http.listen(port, ip);