-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
142 lines (138 loc) · 5.76 KB
/
server.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
// Dependencies initialization
var http = require('http');
var fs = require('fs');
var methods = Object.create(null);
// Initializing HTTP server
http.createServer(
function (request, response) {
function respond(code, body, type) {
if (!type) type = 'text/plain';
response.writeHead(code, {
'Content-Type': type,
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'OPTIONS, PUT'
});
if (body && body.pipe) {
body.pipe(response);
} else { response.end(body); }
}
if (request.method in methods) {
methods[request.method](
validatePath(request.url), respond, request
);
} else {
respond(405, 'Method ' + request.method +
' not allowed.');
}
}).listen(8000);
function validatePath(url) {
// -------- Change core folder name ------------
// Repeating until replacement isn't needed
return ('./testdir' + decodeURIComponent(url)).replace(/\/{2,}/g, '/')
.replace(/ /g, '').replace(/[?#].+$/, '');
}
// GET method
methods.GET = function (path, respond, req) {
// If specific JSON required
if (req.url.includes('?type=json')) {
fs.stat(path, function (error, stats) {
if (error && error.code === 'ENOENT') {
respond(404, JSON.stringify({
type: 'error',
data: 'File not found!'
}, null, 4), 'text/plain; charset=utf-8');
} else if (error) {
respond(500, error.toString());
// If directory is requested
} else if (stats.isDirectory()) {
fs.readdir(path, function (error, fileNames) {
if (error) {
respond(500, JSON.stringify({
type: 'error',
data: error.message
}, null, 4), 'text/plain; charset=utf-8');
} else {
// Creating promises for every file requested
var promises = [];
fileNames.forEach(function (fileName) {
promises.push(new Promise(
function (resolve, reject) {
// Checking if file exists
fs.stat(path + '/' + fileName,
function (error, subStats) {
if (error) {
reject(error.message);
} else {
// Sending file/dir name
resolve({
type: subStats.isDirectory() ? 'dir' : 'file',
name: fileName
});
}
});
}));
});
Promise.all(promises).then(function (data) {
// console.log(data);
respond(200, JSON.stringify({
type: 'dir',
// Sorting files in alphabetic order
data: data.sort(function (a, b) {
if (a.name > b.name) return 1;
return 0;
// Displaying folders on top of list
}).sort(function (a, b) {
if (a.type !== b.type) return -1;
return 0;
})
}, null, 4), 'text/plain; charset=utf-8');
}).catch(function (message) {
respond(500, JSON.stringify({
type: 'error',
data: message
}, null, 4), 'text/plain; charset=utf-8');
});
}
});
} else {
fs.readFile(path, function read(error, data) {
// Responding with error JSON
if (error) {
respond(500, JSON.stringify({
type: 'error',
data: error.message
}, null, 4), 'text/plain; charset=utf-8');
}
// Responding with data JSON
respond(200, JSON.stringify({
type: 'file',
data: data.toString()
}, null, 4), 'text/plain; charset=utf-8');
});
}
});
} else {
// Displaying whole page
fs.readFile('./Task39.html', function read(error, data) {
if (error) {
respond(500, error.toString());
}
respond(200, data, 'text/html; charset=utf-8');
});
}
};
// PUT method function
methods.PUT = function (path, respond, req) {
var outStream = fs.createWriteStream(path);
outStream.on('error', function (error) {
respond(500, JSON.stringify({
ok: false,
error: error.message
}, null, 4), 'text/plain; charset=utf-8');
});
outStream.on('finish', function () {
respond(204, JSON.stringify({
}, null, 4), 'text/plain; charset=utf-8');
});
req.pipe(outStream);
};