-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
51 lines (42 loc) · 1.37 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
var http = require('http');
var fs = require('fs');
var formidable = require("formidable");
var util = require('util');
var processAllFieldsOfTheForm = function (req, res) {
var form = new formidable.IncomingForm();
form.parse(req, function (err, fields, files) {
res.writeHead(200, {
'content-type': 'text/html'
});
var text = fieldsToHtmlIfKeys(fields);
res.write(text);
res.end(`<pre>${util.inspect({
fields: fields,
files: files
})}</pre>`);
});
};
var fieldsToHtmlIfKeys = function (fields) {
if (!fields) return;
var text = '';
var borderStyle = '';
if (fields.color) {
text += `<div style="color:${fields.color};">`;
borderStyle = `border: 5px ${fields['border-style'] || 'inset'} ${fields.color};`;
}
if (fields.name) {
var tag = fields.tag || 'h1';
text += `<${tag}>${fields.name}</${tag}>`;
}
if (fields.color) {
text += '</div>';
}
if (fields.image) {
text += `<br /><div><img src=${fields.image} style="width:33%; ${borderStyle}"/></div><br /><br /><br />`;
}
return text;
}
var server = http.createServer(processAllFieldsOfTheForm);
var port = process.env.PORT || 3000;
server.listen(port);
console.log(`Server listening on ${port}`);