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: 0x05-Node_JS_basic/README.md
+105Lines changed: 105 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -373,3 +373,108 @@ Number of students in SWE: 4. List: Guillaume, Joseph, Paul, Tommy
373
373
bob@dylan:~$
374
374
```
375
375
376
+
[8. Organize a complex HTTP server using Express](./full_server)
377
+
378
+
Obviously writing every part of a server within a single file is not sustainable. Let’s create a full server in a directory named `full_server`.
379
+
380
+
Since you have used ES6 and Babel in the past projects, let’s use `babel-node` to allow to use ES6 functions like `import` or `export`.
381
+
382
+
#### 8.1 Organize the structure of the server
383
+
* Create `2` directories within:
384
+
* `controllers`
385
+
* `routes`
386
+
* Create a file `full_server/utils.js`, in the file create a function named `readDatabase` that accepts a file path as argument:
387
+
* It should read the database asynchronously
388
+
* It should return a promise
389
+
* When the file is not accessible, it should reject the promise with the error
390
+
* When the file can be read, it should return an object of arrays of the firstname of students per fields
391
+
392
+
#### 8.2 Write the App controller
393
+
Inside the file `full_server/controllers/AppController.js`:
394
+
395
+
* Create a class named `AppController`. Add a static method named `getHomepage`
396
+
* The method accepts `request` and `response` as argument. It returns a `200` status and the message `Hello Holberton School!`
397
+
398
+
#### 8.3 Write the Students controller
399
+
Inside the file `full_server/controllers/StudentsController.js`, create a class named `StudentsController`. Add two static methods:
400
+
401
+
The first one is `getAllStudents`:
402
+
403
+
* The method accepts `request` and `response` as argument
404
+
* It should return a status `200`
405
+
* It calls the function `readDatabase` from the `utils` file, and display in the page:
406
+
* **First line**: `This is the list of our students`
407
+
* And for each field (*order by alphabetic order case insensitive*), a line that displays the number of students in the field, and the list of first names (*ordered by appearance in the database file*) with the following format: `Number of students in FIELD: 6. List: LIST_OF_FIRSTNAMES`
408
+
* If the database is not available, it should return a status `500` and the error message `Cannot load the database`
409
+
410
+
The second one is getAllStudentsByMajor:
411
+
412
+
* The method accepts request and response as argument
413
+
* It should return a status 200
414
+
* It uses a parameter that the user can pass to the browser major. The major can only be CS or SWE. If the user is passing another parameter, the server should return a 500 and the error Major parameter must be CS or SWE
415
+
* It calls the function readDatabase from the utils file, and display in the page the list of first names for the students (ordered by appearance in the database file) in the specified field List: LIST_OF_FIRSTNAMES_IN_THE_FIELD
416
+
* If the database is not available, it should return a status 500 and the error message Cannot load the database
417
+
418
+
#### 8.4 Write the routes
419
+
Inside the file `full_server/routes/index.js`:
420
+
421
+
* Link the route `/` to the `AppController`
422
+
* Link the route `/students` and `/students/:major` to the `StudentsController`
423
+
424
+
#### 8.5 Write the server reusing everything you created
425
+
Inside the file named `full_server/server.js`, create a small `Express` server:
426
+
427
+
* It should use the routes defined in `full_server/routes/index.js`
428
+
* It should use the port `1245`
429
+
430
+
#### 8.6 Update `package.json` (if you are running it from outside the folder full_server)
431
+
If you are starting node from outside of the folder `full_server`, you will have to update the command `dev` by: `nodemon --exec babel-node --presets babel-preset-env ./full_server/server.js ./database.csv`
432
+
433
+
##### Warning:
434
+
435
+
* Don’t forget to export your express app at the end of `server.js` (`export default app;`)
436
+
* The database filename is passed as argument of the `server.js` BUT, for testing purpose, you should retrieve this filename at the execution (when `getAllStudents` or g`etAllStudentsByMajor` are called for example)
0 commit comments