Skip to content

Commit 91595ee

Browse files
committed
Update README with details of task 8
1 parent 5898020 commit 91595ee

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed

0x05-Node_JS_basic/README.md

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,3 +373,108 @@ Number of students in SWE: 4. List: Guillaume, Joseph, Paul, Tommy
373373
bob@dylan:~$
374374
```
375375

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)
437+
438+
**In terminal 1**:
439+
```
440+
bob@dylan:~$ npm run dev
441+
...
442+
```
443+
**In terminal 2**:
444+
```
445+
bob@dylan:~$ curl localhost:1245 && echo ""
446+
Hello Holberton School!
447+
bob@dylan:~$
448+
bob@dylan:~$ curl localhost:1245/students && echo ""
449+
This is the list of our students
450+
Number of students in CS: 6. List: Johann, Arielle, Jonathan, Emmanuel, Guillaume, Katie
451+
Number of students in SWE: 4. List: Guillaume, Joseph, Paul, Tommy
452+
bob@dylan:~$
453+
bob@dylan:~$ curl localhost:1245/students/SWE && echo ""
454+
List: Guillaume, Joseph, Paul, Tommy
455+
bob@dylan:~$
456+
bob@dylan:~$ curl localhost:1245/students/French -vvv && echo ""
457+
* Trying 127.0.0.1...
458+
* TCP_NODELAY set
459+
* Connected to localhost (127.0.0.1) port 1245 (#0)
460+
> GET /students/SWES HTTP/1.1
461+
> Host: localhost:1245
462+
> User-Agent: curl/7.58.0
463+
> Accept: */*
464+
>
465+
< HTTP/1.1 500 Internal Server Error
466+
< X-Powered-By: Express
467+
< Date: Mon, 06 Jul 2020 03:29:00 GMT
468+
< Connection: keep-alive
469+
< Content-Length: 33
470+
<
471+
* Connection #0 to host localhost left intact
472+
Major parameter must be CS or SWE
473+
bob@dylan:~$
474+
```
475+
If you want to add test to validate your integration, you will need to add this file: `.babelrc`
476+
```
477+
{
478+
"presets": [["env", {"exclude": ["transform-regenerator"]}]]
479+
}
480+
```

0 commit comments

Comments
 (0)