|
| 1 | +// init project |
| 2 | +var express = require("express"); |
| 3 | +const MongoClient = require("mongodb").MongoClient; |
| 4 | +var db; |
| 5 | +var app = express(); |
| 6 | +var bodyParser = require("body-parser"); |
| 7 | + |
| 8 | +// Add MONGODB_URL and DB_NAME environment variables |
| 9 | +// In MongoDB Atlas (https://cloud.mongodb.com/), from Clusters > Connect > Connect Your Application, copy the URL in 'Connection String Only' |
| 10 | +// Add it and the database name to the environment variables under "Secret Keys' from the server control panel to the left of the editor. |
| 11 | +const client = new MongoClient(process.env.MONGODB_URL, { |
| 12 | + useNewUrlParser: true |
| 13 | +}); |
| 14 | + |
| 15 | +// Using `public` for static files: http://expressjs.com/en/starter/static-files.html |
| 16 | +app.use(express.static("public")); |
| 17 | + |
| 18 | +// Initial set of users to populate the database with |
| 19 | +var defaultUsers = ["Brad Pitt", "Ed Norton", "Denzel Washington"]; |
| 20 | +var users = defaultUsers.slice(); |
| 21 | + |
| 22 | +// Use bodyParser to parse application/x-www-form-urlencoded form data |
| 23 | +var urlencodedParser = bodyParser.urlencoded({ extended: false }); |
| 24 | + |
| 25 | +// Connect to database and insert default users into users collection |
| 26 | +client.connect(err => { |
| 27 | + var dbUsers = []; |
| 28 | + console.log("Connected successfully to database"); |
| 29 | + |
| 30 | + db = client.db(process.env.DB_NAME); |
| 31 | + |
| 32 | + // Removes any existing entries in the users collection |
| 33 | + db.collection("users").deleteMany({ name: { $exists: true } }, function( |
| 34 | + err, |
| 35 | + r |
| 36 | + ) { |
| 37 | + for (var i = 0; i < users.length; i++) { |
| 38 | + // loop through all default users |
| 39 | + dbUsers.push({ name: users[i] }); |
| 40 | + } |
| 41 | + // add them to users collection |
| 42 | + db.collection("users").insertMany(dbUsers, function(err, r) { |
| 43 | + console.log("Inserted initial users"); |
| 44 | + }); |
| 45 | + }); |
| 46 | +}); |
| 47 | + |
| 48 | +// Send user data - used by client.js |
| 49 | +app.get("/users", function(request, response) { |
| 50 | + db.collection("users") |
| 51 | + .find() |
| 52 | + .toArray(function(err, users) { |
| 53 | + // finds all entries in the users collection |
| 54 | + response.send(users); // sends users back to the page |
| 55 | + }); |
| 56 | +}); |
| 57 | + |
| 58 | +// Create a new entry in the users collection |
| 59 | +app.post("/new", urlencodedParser, function(request, response) { |
| 60 | + db.collection("users").insert([{ name: request.body.user }], function( |
| 61 | + err, |
| 62 | + r |
| 63 | + ) { |
| 64 | + console.log("Added a user"); |
| 65 | + response.redirect("/"); |
| 66 | + }); |
| 67 | +}); |
| 68 | + |
| 69 | +// Removes users from users collection and re-populates with the default users |
| 70 | +app.get("/reset", function(request, response) { |
| 71 | + var dbUsers = []; |
| 72 | + db.collection("users").deleteMany({ name: { $exists: true } }, function( |
| 73 | + err, |
| 74 | + r |
| 75 | + ) { |
| 76 | + for (var i = 0; i < users.length; i++) { |
| 77 | + // loop through all users |
| 78 | + dbUsers.push({ name: users[i] }); |
| 79 | + } |
| 80 | + // add them to users collection |
| 81 | + db.collection("users").insertMany(dbUsers, function(err, r) { |
| 82 | + console.log("Inserted initial users"); |
| 83 | + response.redirect("/"); |
| 84 | + }); |
| 85 | + }); |
| 86 | +}); |
| 87 | + |
| 88 | +// Serve the root url: http://expressjs.com/en/starter/basic-routing.html |
| 89 | +app.get("/", function(request, response) { |
| 90 | + response.sendFile("/sandbox/views/index.html"); |
| 91 | +}); |
| 92 | + |
| 93 | +// Listen on port 8080 |
| 94 | +var listener = app.listen(8080, function() { |
| 95 | + console.log("Listening on port " + listener.address().port); |
| 96 | +}); |
0 commit comments