forked from bmoreinis/fakenews
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtserver.js
75 lines (64 loc) · 1.82 KB
/
tserver.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 configdb = require('./mydb').configdb;
// Log console messages
var fs = require('fs');
var fpath = "/usr/lib/node_modules/fakenews/tserver.log";
fs.appendFile(fpath, "Logging! -1\n");
// pm2 start tserver.js --merge-logs
// Initiate Server
var server = http.createServer(function(req, resp) {
var body = "";
req.on('data', function (chunk) {
body += chunk;
});
req.on('end', function () {
fs.appendFile(fpath, body + "\n");
var jsonObj = JSON.parse(body);
console.log(jsonObj);
saveUser(jsonObj);
savePage(jsonObj)
.then(function() { resp.end(JSON.stringify({success: true})); })
.catch(function() { resp.end(JSON.stringify({success: false, error: err})); });
})
});
server.listen(8082);
// Connect to DB with Sequelize
var Sequelize = require('sequelize');
var sequelize = new Sequelize(configdb);
sequelize
.authenticate()
.then(function(err) {
fs.appendFile(fpath, "DB Connected!");
})
.catch(function (err) {
fs.appendFile(fpath, "No DB Connection.");
});
// Write to DB
var Users = sequelize.define('users', {
usersName: {
type: Sequelize.STRING,
field: 'name' // Will result in an attribute that is userName when user facing but users_name in the database
}
}, {
freezeTableName: true // Model tableName will be the same as the model name
});
var Pages = sequelize.define('pages', {
pageURL: {
type: Sequelize.STRING,
field: 'url' // Will result in an attribute that is pageURL when user facing but url in the database
}
}, {
freezeTableName: true // Model tableName will be the same as the model name
});
function saveUser(data) {
console.log("saveUser", data);
return Users.create({
usersName: data.name
});
}
function savePage(data) {
console.log("savePage", data);
return Pages.create({
pageURL: data.url
});
}