Created
June 20, 2018 04:44
-
-
Save bmorelli25/4b1b24827ecab71e22cab79ce3084ddf to your computer and use it in GitHub Desktop.
Live Comments server.js
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 app = express() | |
| const bodyParser = require('body-parser') | |
| const cors = require('cors') | |
| const Pusher = require('pusher') | |
| app.use(cors()) | |
| app.use(bodyParser.urlencoded({ extended: true })) | |
| app.use(bodyParser.json()) | |
| const port = process.env.PORT || 8080 | |
| const pusher = new Pusher({ | |
| appId: 'appId', | |
| key: 'key', | |
| secret: 'secret', | |
| cluster: 'eu', | |
| encrypted: true | |
| }) | |
| let comments = [ | |
| { | |
| author: 'robo', | |
| message: 'i totally didn\'t see that coming' | |
| } | |
| ] | |
| /** | |
| * receive new comment from the client | |
| * update the comments array with the new entry | |
| * publish update to Pusher | |
| */ | |
| app.post('/comment', function (req, res) { | |
| const {author, message} = req.body | |
| comments = [...[{author, message}], ...comments] | |
| pusher.trigger('sport-buzz-news', 'new-comment', {author, message}) | |
| res.sendStatus(200) | |
| }) | |
| // send all comments to the requester | |
| app.get('/comments', function (req, res) { | |
| res.json(comments) | |
| }) | |
| app.listen(port, function () { | |
| console.log('Node app is running at localhost:' + port) | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment