-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
109 lines (91 loc) · 2.75 KB
/
app.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
const express = require('express');
const app = express();
const http = require('http');
const server = http.createServer(app);
server.listen(3001);
const socket = require('socket.io');
io = socket(server);
io.on('connection', (socket) => {
console.log(socket.id);
socket.on('SEND_MESSAGE', function(data){
io.emit('RECEIVE_MESSAGE', data);
})
});
const passport = require('passport');
const session = require('express-session');
const bodyParser = require('body-parser');
const env = require('dotenv').load();
app.use(function (req, res, next) {
// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', 'http://localhost:3000');
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader('Access-Control-Allow-Credentials', true);
// Pass to next layer of middleware
next();
});
const logger = require('morgan');
app.use(logger('dev'));
app.use(bodyParser.urlencoded({ extended: true })); /* Check making it true */
app.use(bodyParser.json());
app.use(session({ secret: 'take-a-paws', resave: true, saveUninitialized:true }));
app.use(passport.initialize());
app.use(passport.session());
const models = require('./server/models');
const authRoute = require('./server/routes/auth.js')(app, passport);
require('./server/routes')(app);
require('./server/config/passport/passport.js')(passport, models.User);
app.get('/id/:email', (req, res) => {
models.User.findOne({
where: {
email: req.params.email
}
}).then((user) => {
res.status(200).send({
id: user.id,
firstname: user.firstname
})
})
});
app.get('/users/:id', (req, res) => {
models.User.findOne({
where: {
id: req.params.id
}
}).then((user) => {
res.status(200).send({
id: user.id,
firstname: user.firstname,
lastname: user.lastname,
email: user.email,
username: user.username,
image: user.image,
description: user.description
})
})
});
app.get('/api/images/:id', (req, res) => {
models.User.findOne({
where: {
id: req.params.id
}
}).then((user) => {
res.status(200).send({
image: user.image
})
})
});
app.get('/error', (req, res) => res.status(200).send({
message: "404"
}));
app.get('/', (req, res) => res.status(200).send({
message: "200"
}));
app.get('*', (req, res) => res.status(200).send({
message: "Welcome"
}));
module.exports = app;