Last active
October 6, 2016 03:37
-
-
Save pnakibar/51d3bfe235cc9a6a14e35a883b83171f 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
import express from 'express'; | |
import bodyParser from 'body-parser'; | |
import morgan from 'morgan'; | |
import socketio from 'socket.io'; | |
import http from 'http'; | |
const app = express(); | |
app.use(morgan('dev')); | |
app.use(bodyParser.urlencoded({ extended: true })); | |
app.use(bodyParser.json()); | |
app.get('/', (req, res) => res.json({ hello: 'world!' })); | |
const server = http.createServer(app); | |
const io = socketio(server); | |
let messages = []; // o seu banco de dados! | |
io.on('connection', (socket) => { | |
socket.emit('messages', messages); | |
socket.emit('hello', 'Welcome!'); | |
socket.on('sent message', (message) => { | |
messages.push(message); | |
io.emit('new message', message); | |
}); | |
}); | |
const port = process.env.PORT || 3000; | |
server.listen(port, () => console.log(`[x] Magic happens on port: ${port}`)); | |
export default app; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment