Created
February 28, 2017 22:01
-
-
Save REPTILEHAUS/5fab3dcb30a1bc0ebb52c4e2e13c7bf1 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
| 'use strict'; | |
| const db | |
| const dbconnection = 'mongodb://username:[email protected]:35333/your-database-name'; | |
| const express = require('express') | |
| const app = express() | |
| const bodyParser = require('body-parser') | |
| const http = require('http').Server(app); | |
| const io = require('socket.io')(http); | |
| const MongoClient = require('mongodb').MongoClient | |
| app.use(bodyParser.urlencoded({ extended: true })) | |
| MongoClient.connect(dbconnection, (err, database) => { | |
| if (err) return console.log(err) | |
| db = database | |
| app.listen(3000, () => { | |
| console.log('MONGODB listening on 3000') | |
| }) | |
| }) | |
| function databaseStore(message) { | |
| let storeData = { chatMessage: message, timestamp: new Date().getTime() } | |
| db.collection('chatroom-chats').save(storeData, (err, result) => { | |
| if (err) return console.log(err) | |
| console.log('saved to database') | |
| }) | |
| } | |
| app.get('/', (req, res) => { | |
| // res.sendFile(__dirname + '/index.html') | |
| res.send('REPTILEHAUS Chat Server') | |
| }) | |
| io.on('connection', (socket) => { | |
| console.log('user connected'); | |
| socket.on('disconnect', function() { | |
| console.log('user disconnected'); | |
| }); | |
| socket.on('add-message', (message) => { | |
| io.emit('message', { type: 'new-message', text: message }); | |
| // Function above that stores the message in the database | |
| databaseStore(message) | |
| }); | |
| }); | |
| http.listen(5000, () => { | |
| console.log('Server started on port 5000'); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment