-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
75 lines (65 loc) · 2.38 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
var http = require('http');
var fs = require('fs');
var formidable = require("formidable");
var util = require('util');
const sqlite3 = require('sqlite3').verbose();
let db = new sqlite3.Database('./myinfo.sqlite');
db.run('CREATE TABLE IF NOT EXISTS items (name text, userid integer, medicine text, disease text, remarks text, hourTake integer, minTake integer)');
var server = http.createServer(function (req, res) {
if (req.method.toLowerCase() == 'get') {
displayForm(res);
} else if (req.method.toLowerCase() == 'post') {
//processAllFieldsOfTheForm(req, res);
processFormFieldsIndividual(req, res);
}
});
function displayForm(res) {
fs.readFile('form.html', function (err, data) {
res.writeHead(200, {
'Content-Type': 'text/html',
'Content-Length': data.length
});
res.write(data);
res.end();
});
}
function processAllFieldsOfTheForm(req, res) {
var form = new formidable.IncomingForm();
form.parse(req, function (err, fields, files) {
//Store the data from the fields in your data store.
//The data store could be a file or database or any other store based
//on your application.
res.writeHead(200, {
'content-type': 'text/plain'
});
res.write('received the data:\n\n');
res.end(util.inspect({
fields: fields,
files: files
}));
});
}
function processFormFieldsIndividual(req, res) {
//Store the data from the fields in your data store.
//The data store could be a file or database or any other store based
//on your application.
var fields = [];
var form = new formidable.IncomingForm();
form.on('field', function (field, value) {
console.log(field, " ", value);
fields[field] = value;
});
form.on('end', function () {
res.writeHead(200, {
'content-type': 'text/plain'
});
res.write('received the data:\n\n');
res.end(util.inspect({
fields: fields
}));
db.run("INSERT INTO items (name, userid , medicine, disease, remarks, hourTake, minTake) VALUES (?,?,?,?,?,?,?)", [fields["name"], fields["userid"], fields["medicine"], fields["disease"], fields["remarks"], fields["hour"], fields["minute"]]);
});
form.parse(req);
}
server.listen(1185);
console.log("server listening on 1185");