Skip to content

Commit 03e64e7

Browse files
committed
update markdown style in README for extra code on mongo logger middleware
1 parent fe03bf0 commit 03e64e7

1 file changed

Lines changed: 46 additions & 47 deletions

File tree

Lesson04/mongo_logger_middleware/README.md

Lines changed: 46 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -7,86 +7,85 @@ Install mongodb community and ensure it is running, using the latest instruction
77

88
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:
99

10-
npm install -s mongodb
10+
npm install -s mongodb
1111

1212

1313
Import the mongodb library to `server.js`, and create a variable with our DB url:
1414

15-
// Import MongoDb driver
16-
const MongoClient = require('mongodb').MongoClient;
17-
const dbUrl = "mongodb://localhost:27017/";
15+
// Import MongoDb driver
16+
const MongoClient = require('mongodb').MongoClient;
17+
const dbUrl = "mongodb://localhost:27017/";
1818

1919
Write a database logger middleware function that creates an event info object and inserts it into an events collection:
2020

21-
// Record all events and requests to the server in database here
22-
var dbLogger = function (req, res, next) {
23-
// Create an object to gather data we want to save
24-
let info = {};
25-
// Add route path of request to object
26-
info.path = req.path;
27-
// Add timestamp to info object
28-
info.timestamp = Date.now();
29-
// Add authorization header so we can see associated user
30-
info.token = req.headers.authorization;
31-
// Add the method and body of the request
32-
info.method = req.method;
33-
info.body = req.body;
34-
console.log(info);
35-
// Open a database connection
36-
MongoClient.connect(dbUrl, { useNewUrlParser: true }, function(err, db) {
37-
if (err) {
38-
console.log(err);
39-
return;
21+
// Record all events and requests to the server in database here
22+
var dbLogger = function (req, res, next) {
23+
// Create an object to gather data we want to save
24+
let info = {};
25+
// Add route path of request to object
26+
info.path = req.path;
27+
// Add timestamp to info object
28+
info.timestamp = Date.now();
29+
// Add authorization header so we can see associated user
30+
info.token = req.headers.authorization;
31+
// Add the method and body of the request
32+
info.method = req.method;
33+
info.body = req.body;
34+
console.log(info);
35+
// Open a database connection
36+
MongoClient.connect(dbUrl, { useNewUrlParser: true }, function(err, db) {
37+
if (err) {
38+
console.log(err);
39+
return;
40+
}
41+
var dbo = db.db("mydb");
42+
// Insert our event info object into the events collection
43+
dbo.collection("events").insertOne(info, function(err, res) {
44+
if (err) console.log(err);
45+
db.close();
46+
});
47+
});
48+
// Middleware function done running, move to the next function
49+
next();
4050
}
41-
var dbo = db.db("mydb");
42-
// Insert our event info object into the events collection
43-
dbo.collection("events").insertOne(info, function(err, res) {
44-
if (err) console.log(err);
45-
db.close();
46-
});
47-
});
48-
// 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);
5757

5858
Making sure mongodb is running on your machine, run the program:
5959

60-
npm start
60+
npm start
6161

6262
In another window run a `curl` command to any endpoint, we’ll use `check-in`:
6363

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"
6565

6666
After running a `curl` command like above we should see our event info object logged to the console running our application.
6767

6868
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:
6969

70-
mongo mongodb://localhost:27017/
70+
mongo mongodb://localhost:27017/
7171

7272
This should open up the mongodb command line shell.
7373

7474
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
7676

7777
Assuming you see “mydb” we can open the database with:
7878

7979

80-
use mydb
80+
use mydb
8181

8282
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:
8383

84-
show collections
84+
show collections
8585

8686
If the above command shows the “events” collection we can make a query to view all the entries:
8787

88-
db.events.find()
89-
88+
db.events.find()
9089

9190
The response should include a JSON object for each request that was made after our `dbLogger` was enabled.
9291

0 commit comments

Comments
 (0)