-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
73 lines (63 loc) · 2.17 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
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var db = require('./database')
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.get('/', function (req, res) {
return res.send({ error: true, message: 'Animawka API' })
});
// Retrieve all users
//app.get('/users', function (req, res) {
// dbConn.query('SELECT * FROM users', function (error, results, fields) {
// if (error) throw error;
// return res.json({ error: false, data: results, message: 'users list.' });
// });
//});
app.get('/anime', function (req, res) {
db.getAnimeList().then(results => {
console.log(results);
return res.json({ error: false, data: results[0], message: 'anime list.' });
});
});
app.post('/login', function (req, res) {
const email = req.body.email;
const password = req.body.password;
db.userLogin(email, password).then(out => {
if (out === false) {
return res.json({error:true, token:false, message: 'Incorrect data'});
} else {
console.log(out);
return res.json({error:false, user:out, message: 'Logged'});
}
});
});
app.post('/register', function (req, res) {
const username = req.body.username;
const email = req.body.email;
const password = req.body.password;
const password2 = req.body.password2;
if(username && email && password && password2){
if (password === password2) {
db.userRegister(username, email, password).then(out => {
if (out === false) {
return res.json({error:true, done:false, message: 'Incorrect data'});
} else {
console.log(out);
return res.json({error:false, done:true, message: 'Registered'});
}
});
} else {
return res.json({error:true, done:false, message: 'Incorrect passwords'});
}
} else {
return res.json({error:true, done:false, message: 'Empty data'});
}
});
app.listen(3000, function () {
console.log('Aplication started on port 3000');
});
db.connect();
module.exports = app;