forked from aemal/skype-clone
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
62 lines (48 loc) · 2.4 KB
/
Copy pathapp.js
File metadata and controls
62 lines (48 loc) · 2.4 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
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const mongo = require('mongodb');
const mongoose = require('mongoose');
const passport = require('passport');
const SerialAuthenticator = require('./lib/auth/index');
const Message = require('./lib/models/message.model');
const User = require('./lib/models/user.model');
const db = 'mongodb://localhost:27017/skypeClone';
const authStrategies = {
local : require('./lib/auth/local')(User)
}
const serialAuthenticator = new SerialAuthenticator(User); // pun intended
app.use(require('express-session')({ secret: "FIXME: I should be retrieved from env var ;(", resave: true, saveUninitialized: true }));
app.use(passport.initialize());
app.use(passport.session());
passport.use(authStrategies.local);
passport.serializeUser=serialAuthenticator.serialize;
passport.deserializeUser=serialAuthenticator.deserialize;
//Handlers
const UserHandler = require('./lib/handlers/user.js');
const FriendHandler = require('./lib/handlers/friend.js');
const MessageHandler = require('./lib/handlers/message.js');
const ProfileHandler = require('./lib/handlers/profile.js');
const userHandler = new UserHandler(User);
const friendHandler = new FriendHandler(User);
const messageHandler = new MessageHandler(Message);
const profileHandler = new ProfileHandler(User);
const port = process.env.PORT || 8080;
mongoose.Promise = global.Promise;
mongoose.connection.openUri(db);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.post('/message/send', messageHandler.send.bind(messageHandler));
app.get('/message/get/:id', messageHandler.get.bind(messageHandler));
app.get('/user/get_friends/:id', userHandlercd.get.bind(userHandler));
app.post('/user/register', userHandler.register.bind(userHandler));
app.get('/user/profile/:id', profileHandler.getProfile.bind(profileHandler));
app.post('/user/profile_edit/:id', profileHandler.editProfile.bind(profileHandler));
app.get('/friend/add/:id', friendHandler.add.bind(friendHandler));
app.get('/friend/accept/:id', friendHandler.accept.bind(friendHandler))
app.get('/friend/decline/:id', friendHandler.decline.bind(friendHandler))
app.get('/friend/remove/:id', friendHandler.remove.bind(friendHandler));
app.get('/login/local', passport.authenticate('local'), (req,res) => { res.send('ok') });
app.listen(port, function() {
console.log('Server started on port.....' + port );
});