-
Notifications
You must be signed in to change notification settings - Fork 0
/
iot_home.js
132 lines (120 loc) · 3.33 KB
/
iot_home.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
var http = require('http');
var fs = require('fs');
var path = require('path');
var mime = require('mime');
var formidable = require('formidable');
var cache = {};
var connect = require('connect');
var favicon = require('serve-favicon');
var morgan = require('morgan');
function send404(response) {
response.writeHead(404, {'Content-Type': 'text/plain'});
response.write('Error 404: resource not found.');
response.end();
}
function sendOk(response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.write('OK.');
response.end();
}
function sendFile(response, filePath, fileContents) {
response.writeHead(200, {"content-type": mime.lookup(path.basename(filePath))});
response.end(fileContents);
}
function serveStatic(response, cache, absPath) {
if (cache[absPath]) {
sendFile(response, absPath, cache[absPath]);
} else {
fs.exists(absPath, function(exists) {
if (exists) {
fs.readFile(absPath, function(err, data) {
if (err) {
send404(response);
} else {
cache[absPath] = data;
sendFile(response, absPath, data);
}
});
} else {
send404(response);
}
});
}
}
function doTask(req, res) {
var filePath = false;
switch (req.method) {
case 'GET':
if (req.url == '/') {
filePath = 'public/index.html';
} else {
filePath = 'public' + req.url;
}
var absPath = './' + filePath;
serveStatic(res, cache, absPath);
break;
case 'POST':
switch (req.url) {
case '/file':
process_file(req, res);
break;
case '/link':
case '/seed':
process_small(req, res);
break;
case '/delete':
break;
}
break;
}
}
function process_small(req, res) {
var body = '';
req.setEncoding('utf8');
req.on('data', function(chunk) {
body += chunk;
});
req.on('end', function() {
console.log(body);
var qs = require('querystring');
var val = qs.parse(body);
switch (req.url) {
case '/link':
var link = val.link;
console.log(link);
res.writeHead(301, {Location: link});
res.end();
break;
case '/seed':
var seed = val.seed;
console.log(seed);
res.writeHead(301, {Location: 'http://localhost:3000/'});
res.end();
}
});
}
function copy(src, dst) {
fs.createReadStream(src).pipe(fs.createWriteStream(dst));
}
function process_file(req, res) {
var form = new formidable.IncomingForm();
var realName, tmpName;
form.parse(req, function(err, fields, files) {
copy(files.file.path, 'data/' + files.file.name);
res.end('File upload ok!');
});
}
function isFormData(req) {
var type = req.headers['content-type'] || '';
return 0 == type.indexOf('multipart/form-data');
}
/*
var server = http.createServer(doTask).listen(3000, function() {
console.log("Server listening on port 3000");
});
*/
var app = connect()
.use(favicon('public/favicon.ico'))
.use(morgan('short'))
.use(doTask)
.listen(3000);