-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
68 lines (55 loc) · 1.85 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const express = require('express')
const app = express()
const http = require('http')
const server = http.createServer(app)
const port = 8080
const mongoose = require("mongoose")
const cors = require("cors")
const socketio = require("socket.io")
const { Server } = require("socket.io")
const Chat = require("./schemas/chats")
//const io = new Server(server)
const io = new Server(server, {
cors: { origin: "*" }
});
app.use(cors())
app.get('/potato', (req, res) => {
res.sendFile(__dirname + '/index.html')
})
io.on('connection', (socket) => {
console.log('a user connected');
socket.on('disconnect', () => {
console.log('user disconnected')
});
socket.on('user', function (data) {
console.log(data)
io.emit('user', data)
});
socket.on('chat message', async (msg) => {
console.log('message: ' + msg)
console.log(msg.chat, msg.name)
//await Chat.create({ name: msg.name, message: msg.chat })
io.emit('chat message', msg)
})
socket.broadcast.emit('hi')
})
io.emit('some event', { someProperty: 'some value', otherProperty: 'other value' }); // This will emit the event to all connected sockets
server.listen(3000, () => { console.log('listening on *:3000') })
// routers
const postsRouter = require("./routers/posts")
const testsRouter = require("./routers/tests")
const indexRouter = require("./routers/index")
const commentsRouter = require("./routers/comments")
app.use(express.urlencoded({ extended: false }))
app.use(express.json())
const connect = require("./schemas")
connect()
app.use('/api/posts', postsRouter)
app.use('/test', testsRouter)
const carsRouter = require("./routers/car")
app.use('/cars', carsRouter)
app.use('/api/', indexRouter)
app.use('/api/comments', commentsRouter)
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})