-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
120 lines (107 loc) · 2.94 KB
/
app.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
"use strict";
// Importing modules used.
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var path = require('path');
var redis = require('redis');
var client = redis.createClient();
//Configuring view engine as jade
app.set('view engine', 'jade');
//Importing files used
var conf = require('./server/conf.js');
var utils = require('./server/utils.js');
var notepadLive = require('./server/notepadLive.js');
//Running server on localhost:PORT
server.listen(conf.PORT);
console.log('Server running on '+conf.PORT);
//Serving index.html on localhost:PORT/ url
app.get('/', function (req, res)
{
var path_to_index = path.join(__dirname + conf.PATH_TO_INDEX_TEMPLATE);
notepadLive.get_idCount(utils,conf,client,function(err,idCount)
{
if(err)
{
console.log('ERROR: '+err);
res.send('ERROR');
//error function
}
else
{
var options = {};
options.idCount = idCount;
options.PORT = conf.PORT;
options.baseUrl = conf.baseUrl;
options.submitId_actionUrl = conf.submitId_actionUrl;
options.newId = Number(idCount)+1;
res.render(path_to_index,options);
}
});
});
//Gets id as a get parameter, and returns the corresponding textarea
app.get(conf.submitId_actionUrl,function(req,res)
{
if(!req.query.id)
{
res.send("Error.");
}
else
{
notepadLive.get_idCount(utils,conf,client,function(err,idCount)
{
if(err)
console.log("Error: "+err);
else
{
var path_to_project = path.join(__dirname+conf.PATH_TO_PROJECT_TEMPLATE)
var id = Number(req.query.id);
idCount = Number(idCount);
if(id <= (idCount + 1))
{
if(id == (idCount+1))
{
notepadLive.set_idCount(utils,conf,client,id,function(err)
{
if(err) console.log("Error: "+err);
});
}
var options = {
id : id,
baseUrl : conf.baseUrl,
};
notepadLive.fetch_projectValue(utils,conf,client,id,function(err,value)
{
if(err)
console.log("ERROR: "+err);
else
{
options.value = value;
res.render(path_to_project,options);
}
});
}
else
{
res.send("You can't manually enter id.");
}
}
});
}
});
//Creating a constant live connection
io.on('connection', function (socket)
{
//Listening to event update_textarea, which recieves value of a textarea with given id when it is updated and updates it in db.
socket.on('update_project',function(id,value)
{
console.log("Updating project with id: "+id+" with value: \n"+value+"\n");
utils.set_key(client,conf.dbKey_projectId(id),value,function(err,reply)
{
if(err)
{
console.log("ERROR: "+err);
}
});
});
});