Last active
October 3, 2016 04:34
-
-
Save pnakibar/bca54a06e4c093626e787e23ae1812c6 to your computer and use it in GitHub Desktop.
Express.js em tempo real com Socket.io
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'; // importando o socket.io | |
import http from 'http'; // talvez no seu projeto você não importe a biblioteca http, mas aqui é necessário | |
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!' })); // apenas para verificar se está tudo OK | |
const server = http.createServer(app); // a biblioteca http entra exatamente aqui | |
const io = socketio(server); // associando a instância do socketio com o seu servidor | |
io.on('connection', () => console.log('someone connected')); // imprimindo no console caso alguém entre | |
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