-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
68 lines (62 loc) · 2.31 KB
/
server.js
File metadata and controls
68 lines (62 loc) · 2.31 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
var http = require('http');
var fs = require('fs');
var path = require('path');
var url = require('url');
var querystring = require('querystring');
var server = http.createServer(function(req, res) {
var method = req.method;
var urls = url.parse(req.url, true);
var pathname = urls.pathname;
var queries = urls.query;
if (method == 'GET') {
if (pathname == '/favicon.ico') {
res.writeHead(404, 'Not found');
res.end();
} else if (pathname == '/' || pathname == '/list') {
fs.readFile('list.html', 'binary', function (err, data) {
if (err) throw err;
res.writeHead(200, {'Content-Type':'text/html'});
res.write(data, 'binary');
res.end();
});
} else if (pathname == '/jquery.js') {
fs.readFile('jquery.js', 'binary', function (err, data) {
if (err) throw err;
res.writeHead(200, {'Content-Type':'text/html'});
res.write(data, 'binary');
res.end();
});
} else if (pathname == '/listall') {
fs.readdir('blogs', function (err, files) {
if (err) throw err;
var lists = new Array();
for (var i = 0; i < files.length; ++i) {
var file = files[i];
var data = {};
data.title = file;
data.content = fs.readFileSync('blogs/' + file, 'utf8');
lists.push(data);
}
res.writeHead(200, {'Content-Type':'application/json'});
res.write(JSON.stringify(lists), 'utf8');
res.end();
});
}
}else if (method == 'POST') {
if (pathname = '/new') {
req.on('data', function(data) {
var buf = "" + data;
var b = querystring.parse(buf);
if (b.title) {
fs.writeFile('blogs/' + b.title, b.content, function(err) {
if (err) throw err;
res.writeHead(200, {'Content-Type':'text/plain'});
res.end();
});
}
});
}
}
});
server.listen(1337, '127.0.0.1');
console.log('start at 1337');