Simple Chat Room in NodeJS, expressJS and mongoDB in Docker Swarm
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.1 KiB

4 years ago
// declaring dependencies
const express = require('express');
const bodyParser= require('body-parser')
const app = express();
// Make sure you place body-parser before your CRUD handlers!
app.use(bodyParser.urlencoded({ extended: true }))
// Constants
const PORT = 8080;
const HOST = '0.0.0.0';
var MongoClient = require('mongodb').MongoClient;
var connectionString = "mongodb://10.0.5.3:27017/";
MongoClient.connect(connectionString, { useUnifiedTopology: true })
.then(client => {
console.log('Connected to Database')
const db = client.db('cloudDB')
const quotesCollection = db.collection('mycollection')
app.post('/quotes', (req, res) => {
quotesCollection.insertOne(req.body)
.then(result => {
res.redirect('/')
})
.catch(error => console.error(error))
})
app.get('/', (req, res) => {
const cursor = db.collection('quotes').find().toArray()
.then(results => {
console.log(results)
})
res.sendFile(__dirname + '/index.html')
})
app.listen(PORT, HOST);
})
console.log(`Running on http://${HOST}:${PORT}`);