You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Lesson04/mongo_logger_middleware/README.md
+46-47Lines changed: 46 additions & 47 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,86 +7,85 @@ Install mongodb community and ensure it is running, using the latest instruction
7
7
8
8
Install `mongodb` node.js driver, which is a node package to work with mongodb, this program is different from the database itself. It should be installed with the save option:
9
9
10
-
npm install -s mongodb
10
+
npm install -s mongodb
11
11
12
12
13
13
Import the mongodb library to `server.js`, and create a variable with our DB url:
// Middleware function done running, move to the next function
49
-
next();
50
-
}
51
-
// These two lines from an earlier step need to be run before we use `dbLogger`
52
-
app.use(express.urlencoded({extended: true}));
53
-
app.use(express.json());
54
-
55
-
// Tell our app to user the database logger for all requests
56
-
app.use(dbLogger);
51
+
// These two lines from an earlier step need to be run before we use `dbLogger`
52
+
app.use(express.urlencoded({extended: true}));
53
+
app.use(express.json());
54
+
55
+
// Tell our app to user the database logger for all requests
56
+
app.use(dbLogger);
57
57
58
58
Making sure mongodb is running on your machine, run the program:
59
59
60
-
npm start
60
+
npm start
61
61
62
62
In another window run a `curl` command to any endpoint, we’ll use `check-in`:
63
63
64
-
curl -sd "name=john" -X POST http://localhost:3000/check-in | jq -r ".token"
64
+
curl -sd "name=john" -X POST http://localhost:3000/check-in | jq -r ".token"
65
65
66
66
After running a `curl` command like above we should see our event info object logged to the console running our application.
67
67
68
68
Next we’ll confirm that our info object saved to the database by opening it from the command line and viewing the collections. Open a terminal and run the following:
69
69
70
-
mongo mongodb://localhost:27017/
70
+
mongo mongodb://localhost:27017/
71
71
72
72
This should open up the mongodb command line shell.
73
73
74
74
With the mongodb shell open we can run `show dbs` to list all of our local databases. The list should include the name “mydb” used in the line from our logging function:
75
-
show dbs
75
+
show dbs
76
76
77
77
Assuming you see “mydb” we can open the database with:
78
78
79
79
80
-
use mydb
80
+
use mydb
81
81
82
82
Now that we’re using “mydb” we can look at the collections it contains. The list should include the name “events” used in our logging function:
83
83
84
-
show collections
84
+
show collections
85
85
86
86
If the above command shows the “events” collection we can make a query to view all the entries:
87
87
88
-
db.events.find()
89
-
88
+
db.events.find()
90
89
91
90
The response should include a JSON object for each request that was made after our `dbLogger` was enabled.
0 commit comments