-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
122 lines (95 loc) · 3.49 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
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
120
121
122
const express = require('express');
const cookieParser = require('cookie-parser');
const PORT = process.env.PORT || 3000;
const home = require('./routes/home.js');
// const recipesAll = require('./routes/recipesAll.js');
const recipeDelete = require('./routes/recipeDelete.js');
// const recipeEdit = require('./routes/recipeEdit.js');
const recipesMine = require('./routes/recipesMine.js');
const recipeWrite = require('./routes/recipeWrite.js');
const logIn = require('./routes/logIn.js');
const logOut = require('./routes/logOut.js');
const signUp = require('./routes/signUp.js');
const userDelete = require('./routes/userDelete.js');
const userEdit = require('./routes/userEdit.js');
const multer = require('multer');
const server = express();
const auth = require('./auth.js');
const model = require('./database/model.js');
const { buildPage } = require('./template.js');
const upload = multer();
const MAX_SIZE = 1000 * 1000 * 5; // 5 megabytes
const ALLOWED_TYPES = ['image/jpeg', 'image/png'];
server.use(express.urlencoded({ extended: false }));
server.use(express.static('./public'));
server.use(cookieParser(process.env.COOKIE_SECRET));
server.get('/', home.get);
server.post('/', upload.single('avatar'), (request, response) => {
const file = request.file;
//file.mimetype tells us what kind of file it was
if (!ALLOWED_TYPES.includes(file.mimetype)) {
response
.status(400)
.send('<h1>File upload error</h1><p>Please upload an image file</p>');
}
//file.size tells us how big the file was (in bytes)
if (file.size > MAX_SIZE) {
response
.status(400)
.send('<h1>File upload error</h1><p>Profile picture must be < 5MB</p>');
} else {
const { name, email, password } = request.body;
console.log(file.buffer);
auth
.createUser(name, email, password, file.buffer)
.then(auth.saveUserSession)
.then((sid) => {
response.cookie('sid', sid, auth.COOKIE_OPTIONS);
response.redirect('/');
})
.catch((error) => {
console.warn(error);
response.send(buildPage(`Error`, `<h2> Couldn't sign up, sorry</h2>`));
});
}
});
// e.g. request from an img tag
// <img src="/user/3/avatar">
server.get('/user/:id/avatar', (req, res) => {
model
.getAvatar(req.params.id)
.then((user) => {
res.send(user.avatar);
})
.catch((error) => {
console.warn(error);
res.send(
buildPage(`Error`, `<h2> Couldn't handle your avatar, sorry</h2>`)
);
});
});
//server.get('/recipeDelete', recipeDelete.get);
//server.post('/recipeDelete', recipeDelete.post);
//server.get('/recipeEdit', recipeEdit.get);
//server.post('/recipeEdit', recipeEdit.post);
server.get('/recipesMine', recipesMine.get);
//server.post('/recipesMine', recipesMine.post);
server.get('/logIn', logIn.get);
server.post('/logIn', logIn.post);
server.get('/signUp', signUp.get);
server.post('/signUp', signUp.post);
server.post('/logOut', logOut.post);
server.get('/recipeWrite', recipeWrite.get);
server.post('/recipeWrite', recipeWrite.post);
// // upload.single('avatar'),
server.get('/recipesMine', recipesMine.get);
// server.post('/recipesMine', recipesMine.post);
//server.get('/recipeDelete', recipeDelete.get);
//server.post('/recipeDelete', recipeDelete.post);
//server.get('/recipeEdit', recipeEdit.get);
//server.post('/recipeEdit', recipeEdit.post);
server.post('/userDelete', userDelete.post);
server.post('/recipeDelete', recipeDelete.post);
server.get('/userEdit', userEdit.get);
server.post('/userEdit', userEdit.post);
server.listen(PORT, () => console.log(`Listening on http://localhost:${PORT}`));