Skip to content

Instantly share code, notes, and snippets.

@bookercodes
Last active June 4, 2018 16:21
Show Gist options
  • Select an option

  • Save bookercodes/8d4dcd36b2cb7697ecf8a296c4a3c21f to your computer and use it in GitHub Desktop.

Select an option

Save bookercodes/8d4dcd36b2cb7697ecf8a296c4a3c21f to your computer and use it in GitHub Desktop.
// ./server.js
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: 'YOUR INSTANCE LOCATOR UNDER THE "CREDENTIALS" HEADER',
key:
'YOUR SECRET KEY UNDER THE "CREDENTIALS" HEADER'
})
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')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment