diff --git a/backend/api/games.js b/backend/api/games.js index 9d5d4451ecc0f5bbb05b93b9bf498ff0a1c03911..c72da36258a2eaf56faddee216a66f4eccb95db8 100644 --- a/backend/api/games.js +++ b/backend/api/games.js @@ -69,5 +69,38 @@ router.get("/gamename/:gameid", (req, res) => { ); }); +router.post("/", (req, res) => { + MongoClient.connect( + connectionUrl, + { useNewUrlParser: true, useUnifiedTopology: true }, + (err, client) => { + // Unable to connect to database + if (err) { + res.sendStatus(500); // Internal server error + return; + } + + const db = client.db("gameWare"); + const collection = "games"; + + name = req.body.name; + + if (!name) { + // Name not provided in body + res.status(400).send("Name not provided"); + return; + } + + db.collection(collection).insertOne({ name }, (err, result) => { + if (err) { + res.sendStatus(500); // Internal server error + return; + } + res.json(result.ops[0]); + }); + } + ); +}); + // Export API routes module.exports = router; diff --git a/backend/index.js b/backend/index.js index 428c61dae4d400d32f81d40646c7ff7e73cfebed..9bfedb31e46a41002466353beaa0f2de09b9fdc1 100644 --- a/backend/index.js +++ b/backend/index.js @@ -26,5 +26,5 @@ app.get("/api", (req, res) => { app.listen(port, () => { // Start server on port console.log("Running express on port " + port); - console.log("Mongo Connection: " + process.env.MONGO_CONNECTION_STRING); + //console.log("Mongo Connection: " + process.env.MONGO_CONNECTION_STRING); });