Skip to content

Commit 7db532e

Browse files
committed
Add README to root of full_server
1 parent 91595ee commit 7db532e

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed
+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# Full Server
2+
3+
### 8.1 Organize the structure of the server
4+
* Create `2` directories within:
5+
* `controllers`
6+
* `routes`
7+
* Create a file `full_server/utils.js`, in the file create a function named `readDatabase` that accepts a file path as argument:
8+
* It should read the database asynchronously
9+
* It should return a promise
10+
* When the file is not accessible, it should reject the promise with the error
11+
* When the file can be read, it should return an object of arrays of the firstname of students per fields
12+
13+
### 8.2 Write the App controller
14+
Inside the file `full_server/controllers/AppController.js`:
15+
16+
* Create a class named `AppController`. Add a static method named `getHomepage`
17+
* The method accepts `request` and `response` as argument. It returns a `200` status and the message `Hello Holberton School!`
18+
19+
### 8.3 Write the Students controller
20+
Inside the file `full_server/controllers/StudentsController.js`, create a class named `StudentsController`. Add two static methods:
21+
22+
The first one is `getAllStudents`:
23+
24+
* The method accepts `request` and `response` as argument
25+
* It should return a status `200`
26+
* It calls the function `readDatabase` from the `utils` file, and display in the page:
27+
* **First line**: `This is the list of our students`
28+
* 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`
29+
* If the database is not available, it should return a status `500` and the error message `Cannot load the database`
30+
31+
The second one is getAllStudentsByMajor:
32+
33+
* The method accepts request and response as argument
34+
* It should return a status 200
35+
* 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
36+
* 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
37+
* If the database is not available, it should return a status 500 and the error message Cannot load the database
38+
39+
### 8.4 Write the routes
40+
Inside the file `full_server/routes/index.js`:
41+
42+
* Link the route `/` to the `AppController`
43+
* Link the route `/students` and `/students/:major` to the `StudentsController`
44+
45+
### 8.5 Write the server reusing everything you created
46+
Inside the file named `full_server/server.js`, create a small `Express` server:
47+
48+
* It should use the routes defined in `full_server/routes/index.js`
49+
* It should use the port `1245`
50+
51+
### 8.6 Update `package.json` (if you are running it from outside the folder full_server)
52+
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`
53+
54+
#### Warning:
55+
56+
* Don’t forget to export your express app at the end of `server.js` (`export default app;`)
57+
* 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)
58+
59+
**In terminal 1**:
60+
```
61+
bob@dylan:~$ npm run dev
62+
...
63+
```
64+
**In terminal 2**:
65+
```
66+
bob@dylan:~$ curl localhost:1245 && echo ""
67+
Hello Holberton School!
68+
bob@dylan:~$
69+
bob@dylan:~$ curl localhost:1245/students && echo ""
70+
This is the list of our students
71+
Number of students in CS: 6. List: Johann, Arielle, Jonathan, Emmanuel, Guillaume, Katie
72+
Number of students in SWE: 4. List: Guillaume, Joseph, Paul, Tommy
73+
bob@dylan:~$
74+
bob@dylan:~$ curl localhost:1245/students/SWE && echo ""
75+
List: Guillaume, Joseph, Paul, Tommy
76+
bob@dylan:~$
77+
bob@dylan:~$ curl localhost:1245/students/French -vvv && echo ""
78+
* Trying 127.0.0.1...
79+
* TCP_NODELAY set
80+
* Connected to localhost (127.0.0.1) port 1245 (#0)
81+
> GET /students/SWES HTTP/1.1
82+
> Host: localhost:1245
83+
> User-Agent: curl/7.58.0
84+
> Accept: */*
85+
>
86+
< HTTP/1.1 500 Internal Server Error
87+
< X-Powered-By: Express
88+
< Date: Mon, 06 Jul 2020 03:29:00 GMT
89+
< Connection: keep-alive
90+
< Content-Length: 33
91+
<
92+
* Connection #0 to host localhost left intact
93+
Major parameter must be CS or SWE
94+
bob@dylan:~$
95+
```
96+
If you want to add test to validate your integration, you will need to add this file: `.babelrc`
97+
```
98+
{
99+
"presets": [["env", {"exclude": ["transform-regenerator"]}]]
100+
}
101+
```

0 commit comments

Comments
 (0)