-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
112 lines (94 loc) · 3.32 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
const express = require("express");
const session = require("express-session");
const cookieParser = require("cookie-parser");
const bodyParser = require('body-parser');
const fs = require('fs');
const app = express();
const port = 3000;
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false , limit:'50mb'}));
// parse application/json
app.use(bodyParser.json());
app.use(session({
secret: 'keyboard cat',
saveUninitialized: true,
cookie: { maxAge: 1000 * 60 * 60 * 24 },
resave: false
}))
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cookieParser());
app.engine('html', require('ejs').renderFile)
app.use("/", express.static("./client"));
//DATABASE STRUCTURE
app.get("/api/:pathToFile", function (req, res) {
console.log("RECEIVED " + req.params.pathToFile);
res.sendFile(__dirname + "/database/" + req.params.pathToFile);
});
app.get("/api/images/:imgName", function (req, res) {
console.log("IMAGE " + req.params.imgName);
res.sendFile(__dirname + "/database/images/" + req.params.imgName);
});
//IMAGE UPLOADING
app.post("/api/post/photo/user/:username", function(req, res) {
console.log("IMAGE RECEIVED FROM + "+req.params.username);
const date = new Date();
const year = date.getFullYear();
// 👇️ getMonth returns integer from 0(January) to 11(December)
const month = date.getMonth() + 1;
const day = date.getDate();
const withHyphens = [year, month, day].join('-');
const newPath = './database/images/'+req.params.username+withHyphens+'.jpg';
const imgdata = req.body.img;
// to convert base64 format into random filename
const base64Data = imgdata.replace(/^data:([A-Za-z-+/]+);base64,/, '');
fs.writeFileSync(newPath, base64Data, {encoding: 'base64'});
//SWITCH JSON
let rawdata = fs.readFileSync('./database/users.json');
let users = JSON.parse(rawdata);
for(let i in users){
let user = users[i];
if(user.name == req.params.username){
users[i].lastImage = req.params.username+withHyphens+'.jpg';
users[i].location[0] = parseFloat(req.body["location[]"][0])
users[i].location[1] = parseFloat(req.body["location[]"][1]);
console.log(users[i].imagePosted);
users[i].imagePosted[users[i].imagePosted.length-1] = true;
console.log(users[i].imagePosted);
}
}
let data = JSON.stringify(users);
fs.writeFileSync('./database/users.json', data);
console.log("SAVED TO "+newPath);
});
// HOME SCREEN
app.get('/', function(req, res) {
if (req.session.loggedIn) {
res.render(__dirname + "/views/index.html", {username: req.session.username});
} else {
res.redirect("/login");
}
res.end();
});
// AUTHENTICATION
app.get("/login", function(req, res) {
res.sendFile(__dirname + "/views/login.html")
})
app.post("/login", function(req, res) {
let username = req.body.username;
if (username) {
req.session.loggedIn = true;
req.session.username = username;
res.redirect('/');
} else {
res.send('Please enter username!');
}
res.end();
});
app.get('/logout', function(req, res) {
req.session.destroy();
res.redirect("/login")
})
app.listen(port, function () {
console.log(`Listening on port ${port}!`);
});