-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
69 lines (55 loc) · 1.94 KB
/
index.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
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const pool = require('./db');
const User = require('./model/User')
const jwt = require('jsonwebtoken');
const uuid = require('uuid')
require('dotenv').config();
const app = express();
const jwtSecretKey = process.env.JWT_SECRET_KEY;
app.use(cors());
app.use(bodyParser.json());
function authenticateToken(req, res, next) {
const token = req.header('Authorization');
if (!token) return res.sendStatus(401);
jwt.verify(token, jwtSecretKey, (err, user) => {
if (err) return res.sendStatus(403);
req.user = user;
next();
});
}
app.post('/api/register', async (req, res) => {
const { name, surname, mail, password } = req.body
const user = new User({ name: name, surname: surname, mail: mail, password: password })
let stat = await user.Register()
res.status(stat.code).json({ message: stat.message, status: stat.code })
})
app.post('/api/login', async (req, res) => {
const { username, password } = req.body
const user = new User()
let stat = await user.Login(username, password)
res.status(stat.code).json({ message: stat.message, status: stat.code })
})
app.get('/api/test', authenticateToken, (req, res) => {
try {
const { data } = req.body;
// const query = 'INSERT INTO your_table_name (data_column_name) VALUES ($1) RETURNING *';
const query = 'select * from users'
// const values = [data];
pool.query(query, (err, result) => {
if (err) {
console.log(err);
} else {
res.status(200).json(result.rows)
}
});
// res.json(result.rows[0]);
} catch (error) {
res.status(500).json({ error: 'Bir hata oluştu.' });
}
});
const port = 4575;
app.listen(port, () => {
console.log(`Sunucu ${port} numaralı bağlantı noktasında dinleniyor.`);
});