-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
38 lines (34 loc) · 945 Bytes
/
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
const express = require('express')
const bodyParser = require('body-parser')
const cors = require('cors')
const Chatkit = require('pusher-chatkit-server')
const chatkit = new Chatkit.default({
instanceLocator: '',
key:
'='
})
const app = express()
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
app.use(cors())
app.post('/users', (req, res) => {
const { username } = req.body
const user = { name: username, id: username }
chatkit
.createUser(user)
.then(() => {
console.log('Created user ', user.name)
res.status(201).json(user)
})
.catch(error => {
if (error.error === 'services/chatkit/user_already_exists') {
console.log('User already exists ', user.name)
res.status(201).json(user)
} else {
console.error(error)
res.status(error.status).json(error)
}
})
})
app.listen(3001)
console.log('Running on port 3001')