Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
216 lines (183 loc) · 7.24 KB
/
Copy pathindex.js
File metadata and controls
216 lines (183 loc) · 7.24 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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
module.exports = function (entryPoint, serverPort, ALLOW_CORS) {
var fs = require('fs')
, http = require('http')
, path = require('path')
, colors = require('colors')
, url = require('url')
, _ = require('lodash')
, functions = require('./functions')
;
var serverDomain = '127.0.0.1'
, serverUrl = 'http://' + serverDomain + ':' + serverPort
, fileExtension = '.json'
, server
;
var reply = function (request, response, status, headers, content, filePath) {
if (!response.finished) {
if(ALLOW_CORS) {
headers["Access-Control-Allow-Origin"] = "*";
headers["Access-Control-Allow-Headers"] = "Origin, X-Requested-With, Content-Type, Accept";
headers['Access-Control-Request-Method'] = '*';
headers['Access-Control-Allow-Methods'] = '*';
headers['Access-Control-Allow-Headers'] = '*';
}
response.writeHead(status, headers);
if (content) {
response.write(content.toString());
}
response.end();
console.log(" -> " + request.method + " " + serverUrl + request.url + ' ' + (status > 299 ? status.toString().red : status.toString().green) + (filePath ? ' -> ' + filePath.cyan : ''));
}
};
server = http.createServer(function (request, response) {
var parsedUrl = url.parse(request.url, true)
, dirPath = entryPoint + parsedUrl.pathname
, filePath = dirPath + fileExtension
;
/*
Redirects all GET requests with the url that ends with / to the same url without /
e.g. http://127.0.0.1:1337/posts/ -> http://127.0.0.1:1337/posts
*/
if (request.method == 'GET' && parsedUrl.pathname.match(/.+\/$/)) {
reply(request, response, 302, {'Location': serverUrl + parsedUrl.pathname.replace(/\/$/, '')});
}
/*
On GET method, checks if file exists
e.g. http://127.0.0.1:1337/posts -> ~/posts.json
*/
else if (request.method == 'GET' && fs.existsSync(filePath)) {
functions.readJSONFile(filePath)
.then(function (content) {
content = JSON.stringify(content);
reply(request, response, 200, {'Content-Type': 'application/json'}, content, filePath);
})
.catch(function (err) {
reply(request, response, 500, {'Content-Type': 'text/plain'}, err + "\n", filePath);
})
}
/*
On GET method, checks if directory exists
e.g. http://127.0.0.1:1337/posts -> ~/posts/
*/
else if (request.method == 'GET' && fs.existsSync(dirPath)) {
functions.readDir(dirPath, fileExtension)
.then(functions.pushContentFiles)
.then(function (content) {
content = JSON.stringify(content);
reply(request, response, 200, {'Content-Type': 'application/json'}, content, dirPath);
})
.catch(function (err) {
reply(request, response, 500, {'Content-Type': 'text/plain'}, err, dirPath);
})
}
/*
On POST method checks if directory exists
*/
else if (request.method == 'POST' && fs.existsSync(dirPath)) {
// Retrieve request content
request.on('data', function (chunk) {
var jsonContent;
// Validate request content as JSON
try {
jsonContent = JSON.parse(chunk.toString());
}
// If not a valid JSON, returns an error 400
catch (e) {
reply(request, response, 400, {'Content-Type': 'text/plain'}, "400 Bad request\n");
}
// Read inside directory
fs.readdir(dirPath, function (err, files) {
// To create a new file, it searches for a new id and a new name
var id = files.length;
do {
jsonContent.id = id;
filePath = dirPath + '/' + id++ + fileExtension
} while (fs.existsSync(filePath));
// Prepare the JSON to be written in the file
var content = JSON.stringify(jsonContent);
// Write or overwrite the content
fs.writeFile(filePath, content, function (err) {
// If something goes wrong, it returns a 500 status error
if (err) {
reply(request, response, 500, {'Content-Type': 'text/plain'}, filePath + ': ' + err + "\n", filePath);
}
// If it is all done, it returns the updated content with a 200 http status
reply(request, response, 200, {'Content-Type': 'application/json'}, content, filePath);
});
})
});
}
/*
On PATCH or PUT method checks if file exists
*/
else if ((request.method == 'PATCH' || request.method == 'PUT') && fs.existsSync(filePath)) {
// Retrieve request content
request.on('data', function (chunk) {
var jsonContent;
// Validate request content as JSON
try {
jsonContent = JSON.parse(chunk.toString());
}
// If not a valid JSON, return an error 400
catch (e) {
reply(request, response, 400, {'Content-Type': 'text/plain'}, "400 Bad request\n");
}
if (request.method == 'PATCH') {
// Validate request content as JSON
functions.readJSONFile(file)
.then(function (content) {
jsonContent = _.extend(content, jsonContent);
})
.catch(function () {
reply(request, response, 500, {'Content-Type': 'text/plain'}, new Error(file + ': invalid JSON'), filePath);
});
}
// Prepare the JSON to be written in the file
var content = JSON.stringify(jsonContent);
// Write or overwrite the content
fs.writeFile(filePath, content, function (err) {
// If something goes wrong, it returns a 500 status error
if (err) {
reply(request, response, 500, {'Content-Type': 'text/plain'}, filePath + ': ' + err + "\n", filePath);
}
// If it is all done, it returns the updated content with a 200 http status
reply(request, response, 200, {'Content-Type': 'application/json'}, content, filePath);
});
});
}
/*
On DELETE method checks if directory exists
e.g. http://127.0.0.1:1337/posts -> ~/posts/
*/
else if (request.method == 'DELETE' && fs.existsSync(filePath)) {
// Delete file
fs.unlink(filePath, function (err) {
// If something goes wrong, it returns a 500 status error
if (err) {
reply(request, response, 500, {'Content-Type': 'text/plain'}, filePath + ': ' + err + "\n", filePath);
}
// If it is all done, it returns an empty content with a 200 http status
reply(request, response, 200, {'Content-Type': 'text/plain'}, null, filePath);
});
}
/*
No directory or file found
*/
else {
reply(request, response, 404, {'Content-Type': 'text/plain'}, "404 Not Found\n");
}
});
function jrsStartServer() {
try {
server.listen(serverPort, serverDomain);
console.log('\nServer running at '.grey + serverUrl.cyan);
console.log('\nServing directory '.grey + path.resolve(entryPoint).toString().cyan);
console.log('\nCORS enabled: '.grey + ALLOW_CORS.toString().cyan);
} catch (err) {
}
server.on('error', function (error) {
console.error('\nServer closed on error.'.red, error);
});
}
return jrsStartServer;
};