|
50 | 50 | "node_modules/express"
|
51 | 51 | ],
|
52 | 52 | "commits": [
|
53 |
| - "e695cb990291db768e441bf977782618977b4943" |
| 53 | + "387dcd134caf778cda3a89f2877f2a860ecb2338" |
54 | 54 | ]
|
55 | 55 | },
|
56 | 56 | "solution": {
|
|
61 | 61 | "npm install"
|
62 | 62 | ],
|
63 | 63 | "commits": [
|
64 |
| - "ebc7ebdd058b676680fed36ef3b45dc52e09d937" |
| 64 | + "f073668da76cd445cbd45c70b0c9fc17274f75e9" |
65 | 65 | ]
|
66 | 66 | },
|
67 | 67 | "content": "NPM install the \"express\" library module version. Use version 4.x."
|
|
76 | 76 | "cd coderoad && npm install"
|
77 | 77 | ],
|
78 | 78 | "commits": [
|
79 |
| - "3e2d170a7998b956f1b36a5080d134cc524bc106" |
| 79 | + "3801736cd3eae1f7688a9e32c482982e7d3446d5" |
80 | 80 | ]
|
81 | 81 | },
|
82 | 82 | "solution": {
|
83 | 83 | "files": [
|
84 | 84 | "src/server.js"
|
85 | 85 | ],
|
86 | 86 | "commits": [
|
87 |
| - "3d4352c61e8cef06e3ae8fbed7ce841b4ae9c2a1" |
| 87 | + "67d05b65948c4dd8b139f4231217ae2a6fb47c7b" |
88 | 88 | ]
|
89 | 89 | },
|
90 | 90 | "content": "Modify the `server.js` file to log \"Hello World\" to the console."
|
|
107 | 107 | "cd coderoad && npm install"
|
108 | 108 | ],
|
109 | 109 | "commits": [
|
110 |
| - "0aa54d19e8db4c35cec6ae9705b6afd45daec9cd" |
| 110 | + "5cf5cbdda4935611b17244954640e3ada9ccd071" |
111 | 111 | ]
|
112 | 112 | },
|
113 | 113 | "solution": {
|
114 | 114 | "files": [
|
115 | 115 | "src/server.js"
|
116 | 116 | ],
|
117 | 117 | "commits": [
|
118 |
| - "b2592e5ae54175c96b26150acbe5fd02633e701f" |
| 118 | + "64b9e820e56da1c66d2d91f554634a65046ec052" |
119 | 119 | ]
|
120 | 120 | },
|
121 | 121 | "content": "Use the `app.get()` method to serve the string \"Hello Express\" to GET requests matching the `/` (root) path.\n\n**Note:** Be sure that your code works by looking at the logs, then see the results in your browser by running `npm start`."
|
|
135 | 135 | "src/views/index.html"
|
136 | 136 | ],
|
137 | 137 | "commits": [
|
138 |
| - "309aeff0b3304cb1f0f703c923838fc5b9b5cc77" |
| 138 | + "82b406dd56b9b331b4a420cc1fc5a1ac150363e7" |
139 | 139 | ]
|
140 | 140 | },
|
141 | 141 | "solution": {
|
142 | 142 | "files": [
|
143 | 143 | "src/server.js"
|
144 | 144 | ],
|
145 | 145 | "commits": [
|
146 |
| - "0d889a2cacdabd7c01df679eead420136c119d51" |
| 146 | + "2ec971de77fb3906c35d20a15d443b46adfc64c3" |
147 | 147 | ]
|
148 | 148 | },
|
149 | 149 | "content": "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.\n\n**Note:** You can edit the solution of the previous challenge or create a new one. If you create a new solution, keep in mind that Express evaluates routes from top to bottom, and executes the handler for the first match. You have to comment out the preceding solution, or the server will keep responding with a string."
|
|
163 | 163 | "src/public/style.css"
|
164 | 164 | ],
|
165 | 165 | "commits": [
|
166 |
| - "fc97249b3ff4eec8b7c0cb75d80af883001dd8b8" |
| 166 | + "3275c9c40773a2905dcb3eff8568fca4add4779c" |
167 | 167 | ]
|
168 | 168 | },
|
169 | 169 | "solution": {
|
170 | 170 | "files": [
|
171 | 171 | "src/server.js"
|
172 | 172 | ],
|
173 | 173 | "commits": [
|
174 |
| - "b07695db6ff40979f65c73aa082af6cb4c5d1a35" |
| 174 | + "d7494510e61bb9fcd3c5c586849f7f5123c60fe5" |
175 | 175 | ]
|
176 | 176 | },
|
177 | 177 | "content": "Mount the `express.static()` middleware for all requests with `app.use()`. The absolute path to the assets folder is `\\_\\_dirname + /public`.\nNow your app should be able to serve a CSS stylesheet. From outside, the public folder will appear mounted to the root directory. Your front-page should look a little better now!"
|
|
191 | 191 | "src/server.js"
|
192 | 192 | ],
|
193 | 193 | "commits": [
|
194 |
| - "3e403d04739a1213d7dd05fbb474eaef74b9589c" |
| 194 | + "e426fb62116723ca366ed3f6942fab1e0ab80975" |
195 | 195 | ]
|
196 | 196 | },
|
197 | 197 | "solution": {
|
198 | 198 | "files": [
|
199 | 199 | "src/server.js"
|
200 | 200 | ],
|
201 | 201 | "commits": [
|
202 |
| - "cd33e8817bd113703039292eeec78547dfd9d158" |
| 202 | + "8e745f1a1958c335c41ac68525084137edd2c911" |
203 | 203 | ]
|
204 | 204 | },
|
205 | 205 | "content": "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."
|
|
219 | 219 | ".env"
|
220 | 220 | ],
|
221 | 221 | "commits": [
|
222 |
| - "174f91909608229c0c4bb4f2a24db20438e8b1a9" |
| 222 | + "f8eb2340b5fc6c4ce1abed1ddda57791d77be42b" |
223 | 223 | ]
|
224 | 224 | },
|
225 | 225 | "solution": {
|
226 | 226 | "files": [
|
227 | 227 | ".env"
|
228 | 228 | ],
|
229 | 229 | "commits": [
|
230 |
| - "87bda677459719853c9ec32fb77338b41f4bdda5" |
| 230 | + "0c38faf96ef07edd9a5ca4f828e15c75be1d4d22" |
231 | 231 | ]
|
232 | 232 | },
|
233 | 233 | "content": "Create a .env file in the root of your project."
|
|
239 | 239 | ".env"
|
240 | 240 | ],
|
241 | 241 | "commits": [
|
242 |
| - "39103db4deb8e5349131d642957e10e8035aac14" |
| 242 | + "1b508c7a7500e1245192e0d7f1a776578175758d" |
243 | 243 | ]
|
244 | 244 | },
|
245 | 245 | "solution": {
|
246 | 246 | "files": [
|
247 | 247 | ".env"
|
248 | 248 | ],
|
249 | 249 | "commits": [
|
250 |
| - "1eec02c107f43a931891b748e100d5677314e54b" |
| 250 | + "2dc634ea8f4653fcaae597afac693bfcb1b51c9b" |
251 | 251 | ]
|
252 | 252 | },
|
253 | 253 | "content": "Add the .env file to your .gitignore file. It should be kept a secret."
|
|
259 | 259 | ".gitignore"
|
260 | 260 | ],
|
261 | 261 | "commits": [
|
262 |
| - "c75b279cb4ffe971f17792bec62940605d76227b" |
| 262 | + "9a76ecd4214538bfa2227304f4c4b5ee42d59245" |
263 | 263 | ]
|
264 | 264 | },
|
265 | 265 | "solution": {
|
266 | 266 | "files": [
|
267 | 267 | ".gitignore"
|
268 | 268 | ],
|
269 | 269 | "commits": [
|
270 |
| - "eea41519a0e788c1a19699d2748fd917fe35ccd5" |
| 270 | + "a3adc3a6f88b9dfc8e00c49117f93d3370be0101" |
271 | 271 | ]
|
272 | 272 | },
|
273 | 273 | "content": "Let's add an environment variable as a configuration option.\nStore the variable `MESSAGE_STYLE=uppercase` in the `.env` file."
|
|
283 | 283 | "node_modules/dotenv"
|
284 | 284 | ],
|
285 | 285 | "commits": [
|
286 |
| - "377ad7015f50011c03092239b552e47da2eb2820" |
| 286 | + "94c5ab81fecd97639a9701f92530e4a64ba921ac" |
287 | 287 | ]
|
288 | 288 | },
|
289 | 289 | "solution": {
|
|
294 | 294 | "npm install"
|
295 | 295 | ],
|
296 | 296 | "commits": [
|
297 |
| - "23e4a88216158b822253dc2f689489044655e9bf" |
| 297 | + "61a24cd6e1d0eac72299575a8b5f5b5fd9bc02f8" |
298 | 298 | ]
|
299 | 299 | },
|
300 | 300 | "content": "Install the dependency for the package \"dotenv\" as a devDependency (`npm install --save-dev module`). The package helps make variables from the .env file available in your code."
|
|
306 | 306 | "src/server.js"
|
307 | 307 | ],
|
308 | 308 | "commits": [
|
309 |
| - "ac6f2ba8409541ee543b5a8047132c2afdc94269" |
| 309 | + "9bbb478f131e6cd7d2d982b8fe48698fc4195cdc" |
310 | 310 | ]
|
311 | 311 | },
|
312 | 312 | "solution": {
|
313 | 313 | "files": [
|
314 | 314 | "src/server.js"
|
315 | 315 | ],
|
316 | 316 | "commits": [
|
317 |
| - "68c21500b10515aa1163edee541ae1d395731b16" |
| 317 | + "93025d8cb2504761288e967e3a6d7960975cf48c" |
318 | 318 | ]
|
319 | 319 | },
|
320 | 320 | "content": "Load dependencies into your server.js by adding the following line to the top of your file:\n\n```js\nrequire(\"dotenv\").config();\n```\n\nYou can test if it works by logging `process.env.MESSAGE_STYLE`."
|
|
326 | 326 | "src/server.js"
|
327 | 327 | ],
|
328 | 328 | "commits": [
|
329 |
| - "4bdca3060bd78a314c0bd27141ebcac2cdc0afec" |
| 329 | + "20a6ce20a72024f71159940a629bbc0231e88152" |
330 | 330 | ]
|
331 | 331 | },
|
332 | 332 | "solution": {
|
333 | 333 | "files": [
|
334 | 334 | "src/server.js"
|
335 | 335 | ],
|
336 | 336 | "commits": [
|
337 |
| - "4f44259d5d34879eb8d9fc23d344198ba46fc7b0" |
| 337 | + "548c797a4eb8c38504888ca5d14320125ab044e5" |
338 | 338 | ]
|
339 | 339 | },
|
340 | 340 | "content": "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\"}`."
|
|
354 | 354 | "src/server.js"
|
355 | 355 | ],
|
356 | 356 | "commits": [
|
357 |
| - "8a3d3755c6981aae7ac2dd55acdb2b2567209978" |
| 357 | + "81591342527790bdfd2ef6cd1f771c9cfb873ee5" |
358 | 358 | ]
|
359 | 359 | },
|
360 | 360 | "solution": {
|
361 | 361 | "files": [
|
362 | 362 | "src/server.js"
|
363 | 363 | ],
|
364 | 364 | "commits": [
|
365 |
| - "a3611328a16dfc645802e65f2c586bc2958f59e9" |
| 365 | + "a2f661d24f12590937720dbc6c437fe7b3a9d896" |
366 | 366 | ]
|
367 | 367 | },
|
368 | 368 | "content": "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.\n\n**Note:** Express evaluates functions in the order they appear in the code. This is true for middleware too. If you want it to work for all the routes, it should be mounted before them."
|
|
382 | 382 | "src/server.js"
|
383 | 383 | ],
|
384 | 384 | "commits": [
|
385 |
| - "8471e27f0b60fabffc6669f41f4897d13eadc628" |
| 385 | + "26ef8a0459494e8a984e912b4cf94acb1ebc062e" |
386 | 386 | ]
|
387 | 387 | },
|
388 | 388 | "solution": {
|
389 | 389 | "files": [
|
390 | 390 | "src/server.js"
|
391 | 391 | ],
|
392 | 392 | "commits": [
|
393 |
| - "00ecccb50142f9bc0ef4f51cfd8d679ceaa90b47" |
| 393 | + "806912d8180e17a6ae196f070bbc3fd4eb170896" |
394 | 394 | ]
|
395 | 395 | },
|
396 | 396 | "content": "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}`.\n**Note:** The test will not pass if you don’t chain the middleware. If you mount the function somewhere else, the test will fail, even if the output result is correct."
|
|
410 | 410 | "src/server.js"
|
411 | 411 | ],
|
412 | 412 | "commits": [
|
413 |
| - "b65b9005e84e49914b23d3af7fb0ca1fb79440a2" |
| 413 | + "e99a6e8ecbf11c1c74334880aa3fe230a9253ca7" |
414 | 414 | ]
|
415 | 415 | },
|
416 | 416 | "solution": {
|
417 | 417 | "files": [
|
418 | 418 | "src/server.js"
|
419 | 419 | ],
|
420 | 420 | "commits": [
|
421 |
| - "9f3798593c30a35089016c5f0baaabcc09c530da" |
| 421 | + "e7aefe2c38ad9cf339a11984a833876e1ead8561" |
422 | 422 | ]
|
423 | 423 | },
|
424 | 424 | "content": "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`."
|
|
438 | 438 | "src/server.js"
|
439 | 439 | ],
|
440 | 440 | "commits": [
|
441 |
| - "f7d1e55b75da43f18be06e1ce3ce93aaa7424c58" |
| 441 | + "1dc7d7f1b7217239c6a4c5880447ba60a91ea8fd" |
442 | 442 | ]
|
443 | 443 | },
|
444 | 444 | "solution": {
|
445 | 445 | "files": [
|
446 | 446 | "src/server.js"
|
447 | 447 | ],
|
448 | 448 | "commits": [
|
449 |
| - "b8b66571f82c80ef5ccb89db77ed3328d640a51d" |
| 449 | + "04c941e13a93e7a04a56e91fb0370650c874dbde" |
450 | 450 | ]
|
451 | 451 | },
|
452 | 452 | "content": "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`.\n\n**Note:** In the following exercise you are going to receive data from a POST request, at the same `/name` route path. If you want, you can use the method `app.route(path).get(handler).post(handler)`. This syntax allows you to chain different verb handlers on the same path route. You can save a bit of typing, and have cleaner code."
|
|
470 | 470 | "node_modules/body-parser"
|
471 | 471 | ],
|
472 | 472 | "commits": [
|
473 |
| - "7e9e2adbce62b5a50f9d876f018e422584e7e55f" |
| 473 | + "590189cd5abcb641349a26e899e8a10f556847d1" |
474 | 474 | ]
|
475 | 475 | },
|
476 | 476 | "solution": {
|
|
481 | 481 | "npm install"
|
482 | 482 | ],
|
483 | 483 | "commits": [
|
484 |
| - "66a0f7b32598bdb8475ddceeedc31f556d0834ea" |
| 484 | + "b2b784a3921967fcf4dd9cef09d5946253707366" |
485 | 485 | ]
|
486 | 486 | },
|
487 | 487 | "content": "Install the `body-parser` module in your `package.json`."
|
|
493 | 493 | "src/server.js"
|
494 | 494 | ],
|
495 | 495 | "commits": [
|
496 |
| - "fcd7871dcf327b7ff68f6d4b2f454a75538c952d" |
| 496 | + "25b7ebb6f46d824fd30bb47abaa461c45f1286a2" |
497 | 497 | ]
|
498 | 498 | },
|
499 | 499 | "solution": {
|
500 | 500 | "files": [
|
501 | 501 | "src/server.js"
|
502 | 502 | ],
|
503 | 503 | "commits": [
|
504 |
| - "eb31793a6fa8087d77ea28b0d41044dee21bc318" |
| 504 | + "3c119a7de77ccb0b9a7bf8f893f4285c0c99399e" |
505 | 505 | ]
|
506 | 506 | },
|
507 | 507 | "content": "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