Last active
June 4, 2018 16:21
-
-
Save bookercodes/8d4dcd36b2cb7697ecf8a296c4a3c21f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ./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