Skip to content
This repository was archived by the owner on Nov 15, 2024. It is now read-only.

Commit dff74d5

Browse files
committed
Initial commit
1 parent d8aa149 commit dff74d5

File tree

5 files changed

+1995
-0
lines changed

5 files changed

+1995
-0
lines changed

package.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "mongodb-database-example-starter",
3+
"version": "1.0.0",
4+
"description": "MongoDB database demo, storing data in MongoDB Atlas.",
5+
"main": "src/index.js",
6+
"scripts": {
7+
"start": "nodemon src/index.js localhost 8080"
8+
},
9+
"dependencies": {
10+
"body-parser": "1.19.0",
11+
"express": "4.17.1",
12+
"mongodb": "3.4.0"
13+
},
14+
"devDependencies": {
15+
"nodemon": "1.18.4"
16+
},
17+
"keywords": [
18+
"mongodb",
19+
"database"
20+
]
21+
}

public/client.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
$(function() {
2+
$.get("/users", function(users) {
3+
users.forEach(function(user) {
4+
$("<li></li>")
5+
.text(user.name)
6+
.appendTo("ul#users");
7+
});
8+
});
9+
});

src/index.js

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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+
});

views/index.html

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>SQLite3 with Sequelize Example Starter</title>
5+
<meta name="description" content="SQLite3 with Sequelize example starter" />
6+
<meta charset="utf-8" />
7+
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
8+
<meta name="viewport" content="width=device-width, initial-scale=1" />
9+
</head>
10+
<body>
11+
<header>
12+
<h1>
13+
Users
14+
</h1>
15+
</header>
16+
17+
<main>
18+
<form
19+
action="/new"
20+
method="POST"
21+
enctype="application/x-www-form-urlencoded"
22+
>
23+
<input
24+
name="user"
25+
type="text"
26+
maxlength="100"
27+
value=""
28+
placeholder="Add a user"
29+
/>
30+
<button type="submit">Add</button>
31+
</form>
32+
<section class="users">
33+
<ul id="users"></ul>
34+
</section>
35+
<p><a href="/reset">Reset</a></p>
36+
</main>
37+
38+
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
39+
<script src="/client.js"></script>
40+
</body>
41+
</html>

0 commit comments

Comments
 (0)