-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
119 lines (95 loc) · 3.24 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
let webapp = require('./webapp.js');
let fs = require('fs');
let registeredUsers = ['dhanu','pavani'];
let TodosHandler = require('./public/scripts/todosHandler.js')
let todosHandler = new TodosHandler('./data/todos.json');
let StaticFileHandler = require('./handlers/staticFileHandler.js');
let LoginHandler = require('./handlers/loginHandler.js');
let fileHandler = new StaticFileHandler('public',fs);
let loginHandler = new LoginHandler('/login.html',[{"userName":"pavani","password":"pavani"},{"userName":"dhanu","password":"dhanu"}]);
todosHandler.loadTodos();
const logger = function(fs,req,res) {
let logs = ['--------------------------------------------------------------',
`${req.method}`,
`${req.url}`,
`${JSON.stringify(req.headers,null,2)}`,
''
].join('\n');
console.log(`${req.method} ${req.url}`);
fs.appendFile('./data/log.json',logs,()=>{});
}
const respondLoginFailed= function(res){
res.setHeader('Set-Cookie',`message=login failed;Max-Age=5`);
res.redirect('/login');
}
const processLogOut = function(req,res){
res.setHeader('Set-Cookie',`sessionId=0,Expires=${new Date(1).toUTCString()}`);
delete req.user;
res.redirect('/index.html');
}
const toHtml = function(todo){
return `<h3><a href= todoForm.html.${todo.id} >${todo.title}</a></h3>`;
}
const processTodos = function(req,res){
let serveResponse = {};
serveResponse.todos = todosHandler.map(toHtml).join('');
console.log(serveResponse);
res.setHeader('Content-Type','text/html');
res.write(serveResponse.todos);
res.end();
}
const storeTodo = function(req,res){
todosHandler.storeTodo(req.body);
res.statusCode = 200;
res.end();
}
const getTodoInformation = function(req,res){
let title = req.cookies.title;
let serveResponse = {};
if(title)
serveResponse = todosHandler.todos.getTodoInfo(title) || {};
res.write(JSON.stringify(serveResponse));
res.end();
}
const homePage = function(req,res){
let homePage = fs.readFileSync('./public/home.html','utf8');
res.setHeader('Content-Type','text/html');
let user = req.user;
res.write(homePage.replace('WELCOME_USER',`WELCOME ${user}`));
res.end();
}
/*=====================================*/
let app = webapp.create();
app.use((req,res)=>{logger(fs,req,res)});
app.use((req,res)=>{
let url = req.url;
if(url.startsWith('/todoForm.html.')){
let id = url.split('.')[2];
let todoPage = fs.readFileSync('./public/todoForm.html','utf8');
let todoInfo = todosHandler.todos.getTodoInfo(id);
console.log(todoInfo);
todoPage = todoPage.replace("TITLE",todoInfo.title);
todoPage= todoPage.replace("DESCRIPTION",todoInfo.description);
todoPage = todoPage.replace("ITEMS",todoInfo.items)||"";
res.setHeader('Content-Type','text/html');
res.write(todoPage);
res.end();
}
return;
});
app.use(loginHandler.getHandler());
app.use(loginHandler.session.getHandler());
app.use((req,res)=>{
if(req.url!='/index.html'&& !req.user)
res.redirect('/index.html');
});
app.get('/',(req,res)=>{
res.redirect('/index.html');
});
app.get('/home',homePage);
app.get('/todos',processTodos);
app.get('/logout',processLogOut);
app.post('/newTodo',storeTodo);
app.get('/todoInfo',getTodoInformation);
app.usePostProcess(fileHandler.getHandler());
module.exports = app;