-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
73 lines (63 loc) · 1.96 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
const http = require('http');
const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const UserSocket = require('./src/database/UserSocket.js');
const FavoriteSocket = require('./src/database/FavoriteSocket.js');
// setup server
const app = express();
const server = http.createServer(app);
app.use(express.static(path.join(__dirname, 'public')));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// for database
const con = mongoose.createConnection('mongodb://localhost/paperQuery');
const userSocket = new UserSocket(con);
const favoriteSocket = new FavoriteSocket(con);
app.post('/user/signup', (req, res) => {
const user = {
username: req.body.username,
password: req.body.password,
updateTime: req.body.updateTime,
};
userSocket.storeUser(user, res);
});
app.post('/user/login', (req, res) => {
const user = {
username: req.body.username,
password: req.body.password,
updateTime: req.body.updateTime,
};
userSocket.checkUser(user, res);
});
app.post('/favorite/add', (req, res) => {
const favorite = {
title: req.body.title,
paperId: req.body.paperId,
url: req.body.url,
username: req.body.username,
};
favoriteSocket.addFavorite(favorite, res);
});
app.post('/favorite/remove', (req, res) => {
const favorite = {
paperId: req.body.paperId,
username: req.body.username,
};
favoriteSocket.removeFavorite(favorite, res);
});
app.post('/favorite/all', (req, res) => {
const { username } = req.body;
favoriteSocket.loadFavoriteList(username, res);
});
app.get('/*', (req, res) => {
res.sendFile(path.join(__dirname, '/public/index.html'), (error) => {
if (error) {
res.status(500).send(error);
}
});
});
const port = 3000;
server.listen(port);
console.log(`Started on port ${port}`);