Skip to content

Commit 540bcd3

Browse files
committed
complete second pass edits
1 parent c72a875 commit 540bcd3

File tree

6 files changed

+103
-16
lines changed

6 files changed

+103
-16
lines changed

config.yml

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
title: Introduction to Node with Express
22
tagline: Learn how to make a basic server with Node.js and Express.js
33
description: Node.js gives you the ability to run JavaScript files on the server-side. Express is a library for Node.js, that allows you to make requests to different "endpoints" and get a response back.
4+
tags:
5+
- Node
6+
- Express
7+
- JavaScript
8+
- JSON
9+
- API
410

511
template:
6-
name: node-express-server-intro
12+
name: node-express-course
713
repo: node-express-template
814
description: An introduction to Node.js and Express.js servers
915
before:
@@ -115,9 +121,4 @@ steps:
115121
issue: 8
116122
- type: respond
117123
with: 08-complete.md
118-
issue: 8
119-
120-
tags:
121-
- Node
122-
- Express
123-
- JavaScript
124+
issue: 8

responses/01-environment.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,10 @@ cd node-express-server-intro
2020
Open the folder you just cloned in your favorite text editor.
2121
You should have a few files already created:
2222
```
23-
middleware.js
2423
.gitignore
2524
server.js
2625
```
27-
The server file will be the main one we use, and middleware will be used near the end. In the .gitignore folder, you should see this line at the top `/node_modules`
26+
The server file will be the main one we use. In the .gitignore folder, you should see this line at the top `/node_modules`
2827

2928
Express (the server library we will install) is a node_module. Rather than track the entire library in our git history, we will create a package.json file, which will allow us to install and save node modules.
3029

@@ -44,7 +43,7 @@ In your package.json file and make sure you have something like this under your
4443
```
4544
The number on the right is the version you downloaded. Since we aren't tracking the actual `node_modules` folder, this section is a reference used to re-install the modules your application depends on.
4645

47-
When you are finished with this section, push your file to github for the next step:
46+
When you are finished with this section, push your file to GitHub for the next step:
4847
```
4948
git add .
5049
git commit -m "initial file setup"

responses/03-get-json-data.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,27 @@ app.get('/users', function(req,res){
1212
})
1313
})
1414
```
15-
Let's save your changes on github:
15+
Overall your file should look like this:
16+
```javascript
17+
const express = require('express');
18+
const app = express();
19+
20+
const mockUserData=[
21+
{name:'Mark'},
22+
{name:'Jill'}
23+
]
24+
25+
app.get('/users',function(req,res){
26+
res.json({
27+
success: true,
28+
message: 'successfully got users. Nice!',
29+
users: mockUserData
30+
})
31+
})
32+
33+
app.listen(8000,function(){console.log('server is listening')})
34+
```
35+
Let's save your changes on GitHub:
1636
```
1737
git add server.js
1838
git commit -m"add first GET route"

responses/04-complete.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
With this last push, your [repository]({{ repoUrl }}) should look [like this]({{ repoUrl }}/tree/04).
22

3-
💡 You can use dynamic variables to get specific data from the database.
3+
💡 You can use dynamic variables to search for specific data associated with an id in your database, and return that (instead of just returning the id).
44

55
[Click here for the next step]({{ repoUrl }}/issues/6)

responses/04-get-data-with-var.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
In Express, words with a colon in front of them in the url are treated as variables. You can access the value of each variable through req.params, like this:
22
```javascript
33
app.get('/users/:id',function(req,res){
4-
console.log(req.params.id)
4+
console.log(req.params.id)
5+
res.json({
6+
success: true,
7+
message: 'got one user',
8+
user: req.params.id
9+
})
510
})
611
```
712

8-
After adding this code to your script.js file, and restarting the server, paste this into your browser url: http://localhost:8000/users/mark
9-
Now check the terminal for the console.log of req.params.id. You should see the name 'mark', since it is the value we passed in place of the 'id' variable.
13+
_Let's test it out!_
1014

11-
Notice that we didn't actually return anything from the server this time, so the browser should appear unresponsive.
15+
Add the function above your `app.listen` function in your server.js file, restart the server, and paste this into your browser url: http://localhost:8000/users/mark
16+
17+
Now check the terminal for the console.log of req.params.id. You should see the name 'mark', since it is the value we passed in place of the 'id' variable. Since we are running this in the terminal, our console.log will appear there (instead of the browser).
1218

1319
After testing that it works, push your changes your GitHub repo:
1420
```

responses/05-post-data.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
So far, we have only made GET requests to our server. A POST request can send data securely through the request body. In order to make POST requests, first we need to include the "body-parser" library from our node_modules (included with express). Add these lines after the app variable:
2+
23
```javascript
34
const bodyParser = require('body-parser');
45
app.use(bodyParser.json());
@@ -26,6 +27,66 @@ app.post('/login',function(req,res){
2627
})
2728
}
2829
})
30+
```
31+
32+
Your entire file should look like this now:
33+
34+
```javascript
35+
const express = require('express');
36+
const app = express();
37+
const bodyParser = require('body-parser');
38+
39+
app.use(bodyParser.json())
40+
41+
const mockUserData=[
42+
{name:'Mark'},
43+
{name:'Jill'}
44+
]
45+
46+
app.get('/users',function(req,res){
47+
res.json({
48+
success: true,
49+
message: 'successfully got users. Nice!',
50+
users: mockUserData
51+
})
52+
})
53+
// colons are used as variables that be viewed in the params
54+
app.get('/users/:id',function(req,res){
55+
console.log(req.params.id)
56+
res.json({
57+
success: true,
58+
message: 'got one user',
59+
user: req.params.id
60+
})
61+
})
62+
63+
app.post('/login',function(req,res){
64+
// Typically passwords are encrypted using something like bcrypt before sending to database
65+
const username=req.body.username;
66+
const password=req.body.password;
67+
68+
// This should come from the database
69+
const mockUsername="billyTheKid";
70+
const mockPassword="superSecret";
71+
72+
if (username===mockUsername && password===mockPassword){
73+
// In practice, use JSON web token sign method here to make an encrypted token
74+
res.json({
75+
success: true,
76+
message: 'password and username match!',
77+
token: 'encrypted token goes here'
78+
})
79+
} else {
80+
res.json({
81+
success: false,
82+
message: 'password and username do not match'
83+
})
84+
}
85+
86+
})
87+
88+
app.listen(8000,function(){console.log('server is listening')})
89+
2990
```
3091
Time to commit our changes!
3192
```

0 commit comments

Comments
 (0)