-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
88 lines (71 loc) · 2.17 KB
/
main.js
File metadata and controls
88 lines (71 loc) · 2.17 KB
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
const express = require("express");
const mysql = require("mysql");
const jwt = require("jsonwebtoken");
const path = require("path");
const bcrypt = require('bcrypt');
const app = express();
app.use(express.json());
const env = process.env
// Read DB connection from env (set by docker-compose)
const dbConfig = {
host: env.DB_HOST,
user: env.DB_USER,
password: env.DB_PASSWORD,
database: env.DB_NAME,
port: 3306,
};
const con = await mysql.createConnection(dbConfig);
function connectWithRetry() {
con.connect(function (err) {
if (err) {
console.error("MySQL connect error:", err.code, err.message);
// Retry after a short delay to allow the DB to finish starting up
setTimeout(connectWithRetry, 2000);
return;
}
console.log("MySQL connected");
});
}
setTimeout(connectWithRetry, 2000); //delay per aspettare che il DB si sia avviato
app.use("/App", express.static(path.join(__dirname, "App")));
app.use("/media", express.static(path.join(__dirname, "media")));
app.get("/", (req, res) => {
res.status(200).redirect("./App/login/login.html")
});
app.get("/feed", (req, res) => {
res.redirect("./App/Home/Home.html")
});
app.get("/signToken", (req,res) =>{
let username = req.header("username")
let token = generateToken(username)
return res.json({ key: token })
////////////////////////////////////////////////////////////////
function generateToken(username) {
return jwt.sign({username},env.TOKEN_SECRET,{expiresIn : "24h"})
}
})
app.post("/signUp", (req,res) => {
let NewUser = req.body
const HASH = HashPswd(NewUser.password)
const QUERY = `INSERT INTO db.USERS (id, username, password, PFP_URL) VALUES(0,${NewUser.username}, '${HASH}', '');`
try{
const [results,fields] = con.query(QUERY);
console.log(results)
console.log(fields)
} catch (err){
console.log(err)
}
async function HashPswd(pswd){
bcrypt.hash(pswd,env.SALT,(err,hash) =>{
if(err) throw err
return hash
})
}
})
app.post("/login", (req, res) => {
res.status(501).json({ error: "not_implemented" });
});
const PORT = 3000
app.listen(PORT, () => {
console.log(`Server listening on http://localhost:${PORT}`);
});