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
Modify the `server.js` file to log "Hello World" to the console.
@@ -110,14 +113,14 @@ setup:
110
113
files:
111
114
- src/server.js
112
115
commits:
113
-
- 'a0e3b03'
116
+
- '7462d28'
114
117
commands:
115
118
- npm install
116
119
solution:
117
120
files:
118
121
- src/server.js
119
122
commits:
120
-
- 'e28b824'
123
+
- '331bdfc'
121
124
```
122
125
123
126
Use the `app.get()` method to serve the string "Hello Express" to GET requests matching the `/` (root) path.
@@ -141,12 +144,12 @@ setup:
141
144
files:
142
145
- src/views/index.html
143
146
commits:
144
-
- 'fdd75dc20aa74f37e9f2ece83dce578c48a430bc'
147
+
- 'd806cc5'
145
148
solution:
146
149
files:
147
150
- src/server.js
148
151
commits:
149
-
- 'ec492d67803067158e810e00e817b20d14cc39b6'
152
+
- '1b04cf8'
150
153
```
151
154
152
155
Send the `/views/index.html` file as a response to GET requests to the `/` path. If you view your live app, you should see a big HTML heading (and a form that we will use later…), with no style applied.
@@ -166,12 +169,12 @@ setup:
166
169
files:
167
170
- src/public/style.css
168
171
commits:
169
-
- '6bfd618b3a7163a34dc7e9547791a36a57d39aa7'
172
+
- 'c2c1b90'
170
173
solution:
171
174
files:
172
175
- src/server.js
173
176
commits:
174
-
- 'fb0fe43203089709173372fa39eeb473eae09e80'
177
+
- 'bf27ac1'
175
178
```
176
179
177
180
Mount the `express.static()` middleware for all requests with `app.use()`. The absolute path to the assets folder is `\_\_dirname + /public`.
@@ -192,12 +195,12 @@ setup:
192
195
files:
193
196
- src/server.js
194
197
commits:
195
-
- '78962a4195d663a06946ea0b1ec62c10fb8ea71b'
198
+
- 'ead9fcb'
196
199
solution:
197
200
files:
198
201
- src/server.js
199
202
commits:
200
-
- 'b16b05d1c408798fb094d40ad196973f436e983f'
203
+
- '31fd254'
201
204
```
202
205
203
206
Serve the object `{"message": "Hello json"}` as a response, in JSON format, to GET requests to the `/json` route. Then point your browser to `your-app-url/json`, you should see the message on the screen.
@@ -215,14 +218,14 @@ The environment variables are accessible from the app as `process.env.VAR_NAME`.
215
218
```config
216
219
setup:
217
220
commits:
218
-
- '8f4234fce7c540b052ea210191a2a2c6a49b1cca'
221
+
- 'da2dfbc'
219
222
watchers:
220
223
- .env
221
224
solution:
222
225
files:
223
226
- .env
224
227
commits:
225
-
- 'f06ecb4f0ad67bdd6be5a09a36dc7a3b921de182'
228
+
- '3037600'
226
229
```
227
230
228
231
Create a .env file in the root of your project.
@@ -234,12 +237,12 @@ setup:
234
237
files:
235
238
- .gitignore
236
239
commits:
237
-
- 'a3577e7b25f8a2d72a271092362f1b7a0582f243'
240
+
- '66a5a9e'
238
241
solution:
239
242
files:
240
243
- .gitignore
241
244
commits:
242
-
- '323233af8c62f5233d8ccdd6b2e760e211957c6f'
245
+
- 'b21bbf7'
243
246
```
244
247
245
248
Add the .env file to your .gitignore file. It should be kept a secret.
@@ -251,12 +254,12 @@ setup:
251
254
files:
252
255
- .env
253
256
commits:
254
-
- 'fabd307b8a77bf3a965dba8f96e7c91112627257'
257
+
- 'b5a291a'
255
258
solution:
256
259
files:
257
260
- .env
258
261
commits:
259
-
- 'aa0fe163477d8459c187e69f4444a3aa75ea6723'
262
+
- '508520c'
260
263
```
261
264
262
265
Let's add an environment variable as a configuration option.
@@ -269,15 +272,15 @@ setup:
269
272
files:
270
273
- package.json
271
274
commits:
272
-
- '906a86d8af50b2d4d2e73442e5aeb2f3b9761532'
275
+
- '45eafdc'
273
276
watchers:
274
277
- package.json
275
278
- node_modules/dotenv
276
279
solution:
277
280
files:
278
281
- package.json
279
282
commits:
280
-
- 'fe9f2d0fd6d3e58a7652b599d0cf477894998481'
283
+
- 'd400723'
281
284
commands:
282
285
- npm install
283
286
```
@@ -291,12 +294,12 @@ setup:
291
294
files:
292
295
- src/server.js
293
296
commits:
294
-
- 'd5440e5cf711625e859d14ebe851e7bbceff8c0d'
297
+
- 'a1f1713'
295
298
solution:
296
299
files:
297
300
- src/server.js
298
301
commits:
299
-
- '75d6643eb1f20049f77077ea96806c99989e2d72'
302
+
- '44e6c3a'
300
303
```
301
304
302
305
Load dependencies into your server.js by adding the following line to the top of your file:
@@ -314,12 +317,12 @@ setup:
314
317
files:
315
318
- src/server.js
316
319
commits:
317
-
- '7cee81b2c2c847718d2b2a123efbe106c84a3ad9'
320
+
- '40fd774'
318
321
solution:
319
322
files:
320
323
- src/server.js
321
324
commits:
322
-
- 'ebfd74be1a47a4348d9acfdcc6d345ab257d572e'
325
+
- 'f5340dc'
323
326
```
324
327
325
328
Tell the GET `/json` route handler that you created in the last challenge to transform the response object’s message to uppercase if `process.env.MESSAGE_STYLE` equals `uppercase`. The response object should become `{"message": "HELLO JSON"}`.
@@ -350,12 +353,12 @@ setup:
350
353
files:
351
354
- src/server.js
352
355
commits:
353
-
- 'e400e1df828926321eb375686e6b5c388342119b'
356
+
- 'b18fa07'
354
357
solution:
355
358
files:
356
359
- src/server.js
357
360
commits:
358
-
- '458f2d51fd7b77be921a197ef2ec5589bbf4e282'
361
+
- '9ba7d21'
359
362
```
360
363
361
364
Build a simple logger. For every request, it should log to the console a string taking the following format: `method path - ip`. An example would look like this: `GET /json - ::ffff:127.0.0.1`. Note that there is a space between `method` and `path` and that the dash separating `path` and `ip` is surrounded by a space on both sides. You can get the request method (http verb), the relative route path, and the caller’s ip from the request object using `req.method`, `req.path` and `req.ip`. Remember to call `next()` when you are done, or your server will be stuck forever. Be sure to have the ‘Logs’ opened, and see what happens when some request arrives.
@@ -391,12 +394,12 @@ setup:
391
394
files:
392
395
- src/server.js
393
396
commits:
394
-
- '6adeb38dc7c4349ee3138be6bef78f7a7b562874'
397
+
- 'b3c244e'
395
398
solution:
396
399
files:
397
400
- src/server.js
398
401
commits:
399
-
- 'ee9205ea772e60bbce0671a9bd6855e7b2d3357f'
402
+
- '5b12fa5'
400
403
```
401
404
402
405
In the route `app.get('/now', ...)` chain a middleware function and the final handler. In the middleware function you should add the current time to the request object in the `req.time` key. You can use `new Date().toString()`. In the handler, respond with a JSON object, taking the structure `{time: req.time}`.
@@ -417,12 +420,12 @@ setup:
417
420
files:
418
421
- src/server.js
419
422
commits:
420
-
- 'ab47c91e36779a9132e0cddc88166aa58b44062b'
423
+
- 'fd4a291'
421
424
solution:
422
425
files:
423
426
- src/server.js
424
427
commits:
425
-
- 'c53edaa512157c7791b01f8e9aca9de719545451'
428
+
- '6d1db55'
426
429
```
427
430
428
431
Build an echo server, mounted at the route `GET /echo/:word`. Respond with a JSON object, taking the structure `{echo: word}`. You can find the word to be repeated at `req.params.word`.
@@ -442,12 +445,12 @@ setup:
442
445
files:
443
446
- src/server.js
444
447
commits:
445
-
- '2111dd1cd4111f113ba3b8071ab80de42ade8844'
448
+
- '1881c49'
446
449
solution:
447
450
files:
448
451
- src/server.js
449
452
commits:
450
-
- '0ed8dd9050558854a90ae10ccd0096a7f1741b76'
453
+
- 'd12bd73'
451
454
```
452
455
453
456
Build an API endpoint, mounted at `GET /name`. Respond with a JSON document, taking the structure `{ name: 'firstname lastname'}`. The first and last name parameters should be encoded in a query string e.g. `?first=firstname&last=lastname`.
@@ -480,15 +483,15 @@ setup:
480
483
files:
481
484
- package.json
482
485
commits:
483
-
- '343c6b6d0cea18b5439fee7ca3babebe91d74ccb'
486
+
- 'e510f57'
484
487
watchers:
485
488
- package.json
486
489
- node_modules/body-parser
487
490
solution:
488
491
files:
489
492
- package.json
490
493
commits:
491
-
- '0835a39f5f5fd38b690ad9c8d1ae6243b3b4dd7c'
494
+
- 'a38dc38'
492
495
commands:
493
496
- npm install
494
497
```
@@ -502,12 +505,12 @@ setup:
502
505
files:
503
506
- src/server.js
504
507
commits:
505
-
- 'b5d65b56bf232ec09c6101b9e868213fb157a32a'
508
+
- '4249499'
506
509
solution:
507
510
files:
508
511
- src/server.js
509
512
commits:
510
-
- '2c3c3815e5352f3a94716d5023af93f1b49fa5cd'
513
+
- '2a74c71'
511
514
```
512
515
513
516
Require "body-parser" at the top of the server.js file. Store it in a variable named `bodyParser`. The middleware to handle urlencoded data is returned by `bodyParser.urlencoded({extended: false})`. Pass to `app.use()` the function returned by the previous method call. As usual, the middleware must be mounted before all the routes which need it.
0 commit comments