Skip to content

Instantly share code, notes, and snippets.

@bmorelli25
Created June 20, 2018 04:44
Show Gist options
  • Select an option

  • Save bmorelli25/4b1b24827ecab71e22cab79ce3084ddf to your computer and use it in GitHub Desktop.

Select an option

Save bmorelli25/4b1b24827ecab71e22cab79ce3084ddf to your computer and use it in GitHub Desktop.
Live Comments server.js
// 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