forked from Waasi/gotenna-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Add dashboard template - Add Mongodb - Create GET & POST endpoints - Save messages via post endpoint "/message" - Retrieve all messages via get endpoint "/"
- Loading branch information
Showing
5 changed files
with
3,005 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
const express = require('express') | ||
const app = express() | ||
const port = 3000 | ||
const MongoClient = require('mongodb').MongoClient; | ||
|
||
// Connection URL | ||
const url = 'mongodb://localhost:27017'; | ||
// Database Name | ||
const dbName = 'gotenacious'; | ||
app.use(express.json()); | ||
app.get('/', (req, res) => { | ||
// Get all messages | ||
MongoClient.connect(url, function(err, client) { | ||
// assert.equal(null, err); | ||
if (err) { | ||
console.error('An error occurred connecting to MongoDB: ', err); | ||
} else { | ||
console.log("Connected successfully to server"); | ||
const db = client.db(dbName); | ||
|
||
getMessages(db, (messages) => { | ||
client.close(); | ||
res.send(messages) | ||
}) | ||
} | ||
|
||
}); | ||
// res.sendFile(__dirname + '/templates/dashboard.html') | ||
}) | ||
|
||
app.post('/message', (req, res) => { | ||
// console.log(req.body) | ||
message = req.body | ||
MongoClient.connect(url, function(err, client) { | ||
// assert.equal(null, err); | ||
if (err) { | ||
console.error('An error occurred connecting to MongoDB: ', err); | ||
} else { | ||
console.log("Connected successfully to server"); | ||
const db = client.db(dbName); | ||
|
||
insertMessage(db, message, () => { | ||
client.close(); | ||
res.send('message added to db') | ||
}) | ||
} | ||
|
||
}); | ||
}) | ||
|
||
app.listen(port, () => console.log(`Dashboard listening on port ${port}!`)) | ||
|
||
|
||
const insertMessage = function(db, message, callback) { | ||
// console.log(rb) | ||
const collection = db.collection('messages') | ||
collection.insertOne(message) | ||
callback(); | ||
} | ||
|
||
const getMessages = function(db, callback) { | ||
// Get the documents collection | ||
const collection = db.collection('messages'); | ||
// Find some documents | ||
collection.find({}).toArray(function(err, messages) { | ||
console.log("Found the following messages"); | ||
console.log(messages) | ||
callback(messages); | ||
}); | ||
} |
Empty file.
Oops, something went wrong.