Snippets
1
2
3
4
5
6
7
8
X
Need Help?
Snippet 1
X
Snippet 2
const express = require("express"); const bodyParser = require("body-parser"); const cors = require("cors"); const app = express(); const port = 3000; app.use(bodyParser.json()); app.use(cors()); app.listen(port, () => console.log("App running on port", port));
X
Snippet 3
const MongoClient = require('mongodb').MongoClient; const url = 'mongodb://localhost:27017'; const dbName = 'messageBoard'; let db; MongoClient.connect(url,{useUnifiedTopology: true}, function(err, client) { if(err) return console.log("mongodb error",err); console.log("Connected successfully to server"); db = client.db(dbName); });
X
Snippet 4
post(){ console.log(this.n,this.m); this.http.post("http://localhost:3000/send", { msg:this.m, name:this.n }).toPromise(); location.reload(); }
X
Snippet 5
app.post("/send", (req,res)=>{ const nameMessage = req.body; db.collection("messages").insertOne(nameMessage); })
X
Snippet 6
app.get("/send", async (req,res)=>{ const dbInfo = await db.collection("messages").find({}).toArray(); if(!dbInfo) return res.json({error:"Error getting messages"}); res.json(dbInfo); })
X
Snippet 7
async ngOnInit(){ this.messages = (await this.http.get("http://localhost:3000/send").toPromise()) as any[]; }
X
Snippet 8
Messages
{{m.name}} says: {{m.msg}}
X