-
Notifications
You must be signed in to change notification settings - Fork 10
/
server.js
70 lines (61 loc) · 1.99 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
const express = require('express');
const connectDB = require('./config/db');
const path = require('path');
const app = express();
const bodyParser = require('body-parser');
const cors = require('cors');
const Chatkit = require('@pusher/chatkit-server');
// Connect Database
connectDB();
// Init Middleware
const chatkit = new Chatkit.default({
instanceLocator: 'v1:us1:050ef61d-32d9-469a-b539-fb49504cc585',
key: 'b5ce6670-7416-49f3-8470-270c884bb97d:y7XlW3P/5sOXOzkjxO4qOrLXRjGbvHp5w9OJhq109nU=',
});
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.json({ extended: false }));
// Define Routes
app.use('/api/users', require('./routes/api/users'));
app.use('/api/auth', require('./routes/api/auth'));
app.use('/api/profile', require('./routes/api/profile'));
app.use('/api/posts', require('./routes/api/posts'));
app.use('/api/contact', require('./routes/api/contact'));
app.use('/api/events', require('./routes/api/events'))
app.post('/users', (req, res) => {
const { userId } = req.body;
chatkit
.createUser({
id: userId,
name: userId,
})
.then(() => {
res.sendStatus(201);
})
.catch(err => {
if (err.error === 'services/chatkit/user_already_exists') {
console.log(`User already exists: ${userId}`);
res.sendStatus(200);
} else {
res.status(err.status).json(err);
}
});
});
app.post('/authenticate', (req, res) => {
const authData = chatkit.authenticate({
userId: req.query.user_id,
});
res.status(authData.status).send(authData.body);
});
// Serve static assets in production
if (process.env.NODE_ENV === 'production') {
// Set static folder
app.use(express.static('client/build'));
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
});
}
const PORT = process.env.PORT || 5000;
// server.listen(process.env.PORT || port)
app.listen(PORT, () => console.log(`Server started on port ${PORT}`));