├── docker-compose.yml
├── LICENSE
└── README.md
/docker-compose.yml:
--------------------------------------------------------------------------------
1 | version: "3.9"
2 | services:
3 | node:
4 | container_name: react-backend-node
5 | image: learnwebcode/react-backend-node:latest
6 | restart: always
7 | user: "node"
8 | working_dir: /home/node/app
9 | environment:
10 | - NODE_ENV=production
11 | ports:
12 | - 8080:8080
13 | command: "npm start"
14 | depends_on:
15 | - mongo
16 | mongo:
17 | container_name: "react-backend-mongo"
18 | image: mongo:5.0.1-focal
19 | restart: always
20 | ports:
21 | - 27017:27017
22 | volumes:
23 | - ./db:/data/db
24 | environment:
25 | MONGO_INITDB_ROOT_USERNAME: root
26 | MONGO_INITDB_ROOT_PASSWORD: root
27 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2021 Brad Schiff
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # You Will Need Docker Installed
2 |
3 | You can visit [the Docker Desktop page to download and install Docker](https://www.docker.com/products/docker-desktop) on your computer.
4 |
5 | # Creating Our Project
6 |
7 | Create a new empty folder anywhere on your computer. Place the `docker-compose.yml` file from this repository into the new empty folder you just created.
8 |
9 | Point your command-line towards this new folder and run the `docker compose up -d` command. You only need to run this command this one time for the lifetime of this folder/project.
10 |
11 | From now on, whenever you want to stop the app you can run `docker compose stop` and when you want to start it up again you can run `docker compose start`.
12 |
13 | You can now visit `http://localhost:8080/` in a browser and should see a "Hello" message. Look further down on this page for information about all API Endpoints / URLs.
14 |
15 | # Easiest Way To Send Practice Requests
16 |
17 | Eventually you'll want to program your requests within your own application, but while you're practicing or debugging it is nice to have a GUI app for sending requests.
18 |
19 | While Postman is popular, I strongly prefer using [Insomnia](https://insomnia.rest/download) instead. Insomnia is free to use for Windows, Mac, and Linux.
20 |
21 | # Viewing The Database Directly
22 |
23 | Your project folder now contains a folder named `db` which stores the actual database data.
24 |
25 | You can visit [the official MongoDB Compass page](https://www.mongodb.com/try/download/compass) to download and install MongoDB Compass on your computer.
26 |
27 | Once you've opened the MongoDB Compass app, you can use the following connection string to connect to our API's database:
28 |
29 | `mongodb://root:root@localhost:27017/`
30 |
31 | The `ourApp` database in particular is powering our API.
32 |
33 | # API Endpoints
34 |
35 |
36 | Register a New Account
37 |
38 | **POST** `http://localhost:8080/register`
39 |
40 | ### Input
41 |
42 | `{"username": "brad", "email": "example@example", "password": "qwertyqwerty"}`
43 |
44 | ### Output
45 |
46 | Will return an array of reasons why registration failed (e.g. username is already taken, password must be at least 12 characters etc...). If successful, will return an object with the following properties: token, username, avatar.
47 |
48 |
49 |
50 | Does Username Exist?
51 |
52 | **POST** `http://localhost:8080/doesUsernameExist`
53 |
54 | ### Input
55 |
56 | {"username": "barksalot"}
57 |
58 | ### Output
59 |
60 | Returns `true` if the name is already taken; returns `false` if the name is available.
61 |
62 |
63 |
64 | Does Email Exist?
65 |
66 | **POST** `http://localhost:8080/doesEmailExist`
67 |
68 | ### Input
69 |
70 | {"email": "barksalot@example.com"}
71 |
72 | ### Output
73 |
74 | Returns `true` if the email is already in use; returns `false` if the email has not been used yet.
75 |
76 |
77 |
78 | Login
79 |
80 | **POST** `http://localhost:8080/login`
81 |
82 | ### Input
83 |
84 | `{"username": "barksalot", "password": "qwertyqwerty"}`
85 |
86 | ### Output
87 |
88 | Failed login attempts simply return false. If successful, will return an object with the following properties: token, username, avatar.
89 |
90 |
91 |
92 | Create New Post
93 |
94 | **POST** `http://localhost:8080/create-post`
95 |
96 | ### Input
97 |
98 | `{"title": "Being a Dog is Wonderful", "body": "Bark bar bark lorem ipsum. Lorem ipsum.", "token": "yourValidTokenValueHere"}`
99 |
100 | ### Output
101 |
102 | Will return an array of reasons why post creation failed (e.g. You must provide a title, etc..."). If successful, returns the ID value of the newly created post.
103 |
104 |
105 |
106 | View Single Post
107 |
108 | **GET** `http://localhost:8080/post/6122b229996cb5001293d2c4`
109 |
110 | ### Output
111 |
112 | Will return false if post does not exist. If post exists this endpoint will return an object with the following example shape:
113 |
114 | ```
115 | {
116 | "_id": "6122b229996cb5001293d2c4",
117 | "title": "My New Post Today",
118 | "body": "Lorem ipsum dolor sit amet lorem ipsum.",
119 | "createdDate": "2021-08-22T20:23:05.653Z",
120 | "author": {
121 | "username": "barksalot",
122 | "avatar": "https://gravatar.com/avatar/b9216295c1e3931655bae6574ac0e4c2?s=128"
123 | },
124 | "isVisitorOwner": false
125 | }
126 | ```
127 |
128 |
129 |
130 | Edit / Update An Existing Post
131 |
132 | **POST** `http://localhost:8080/post/6122b3ea996cb5001293d2c7/edit`
133 |
134 | ### Input
135 |
136 | ```
137 | {"token": "yourValidTokenHere", "title": "Being a Dog is Great!!!!!", "body": "Woof woof"}
138 | ```
139 |
140 | ### Output
141 |
142 | Will return "success" or "failure" accordingly.
143 |
144 |
145 |
146 | Delete An Existing Post
147 |
148 | **DELETE** `http://localhost:8080/post/6122b50e996cb5001293d2c9`
149 |
150 | ### Input
151 |
152 | ```
153 | {"token": "yourValidTokenHere"}
154 | ```
155 |
156 | ### Output
157 |
158 | Will return "success" or "You do not have permission to perform that action." accordingly.
159 |
160 |
161 |
162 | Start Following a User
163 |
164 | **POST** `http://localhost:8080/addFollow/barksalot`
165 |
166 | ### Input
167 |
168 | ```
169 | {"token": "yourValidTokenHere"}
170 | ```
171 |
172 | ### Output
173 |
174 | Will return true if successful. Will return false if you try to follow yourself or a user you are already following.
175 |
176 |
177 |
178 | Stop Following a User
179 |
180 | **POST** `http://localhost:8080/removeFollow/barksalot`
181 |
182 | ### Input
183 |
184 | ```
185 | {"token": "yourValidTokenHere"}
186 | ```
187 |
188 | ### Output
189 |
190 | Will return true if successful. Will return false if you try to stop following someone you are not currently following.
191 |
192 |
193 |
194 | Get Feed of Posts From Users You Follow
195 |
196 | **POST** `http://localhost:8080/getHomeFeed`
197 |
198 | ### Input
199 |
200 | ```
201 | {"token": "yourValidTokenHere"}
202 | ```
203 |
204 | ### Output
205 |
206 | Will return an array of posts from the users you follow, for example:
207 |
208 | ```
209 | [
210 | {
211 | "_id": "6122b3ea996cb5001293d2c7",
212 | "title": "Being a Dog is Great!!!!!",
213 | "body": "Woof woof",
214 | "createdDate": "2021-08-22T20:30:34.387Z",
215 | "author": {
216 | "username": "barksalot",
217 | "avatar": "https://gravatar.com/avatar/b9216295c1e3931655bae6574ac0e4c2?s=128"
218 | },
219 | "isVisitorOwner": false
220 | },
221 | {
222 | "_id": "6122b229996cb5001293d2c4",
223 | "title": "My New Post Today",
224 | "body": "Lorem ipsum!",
225 | "createdDate": "2021-08-22T20:23:05.653Z",
226 | "author": {
227 | "username": "barksalot",
228 | "avatar": "https://gravatar.com/avatar/b9216295c1e3931655bae6574ac0e4c2?s=128"
229 | },
230 | "isVisitorOwner": false
231 | }
232 | ]
233 | ```
234 |
235 |
236 |
237 | General Profile Info (Counts etc...)
238 |
239 | **POST** `http://localhost:8080/profile/barksalot`
240 |
241 | ### Input
242 |
243 | ```
244 | {"token": "yourValidTokenHere"}
245 | ```
246 |
247 | ### Output
248 |
249 | Will return information about the requested user account. You must send your token along so the system knows if you are already following the user or not. Here is an example of the object shape it returns:
250 |
251 | ```
252 | {
253 | "profileUsername": "barksalot",
254 | "profileAvatar": "https://gravatar.com/avatar/b9216295c1e3931655bae6574ac0e4c2?s=128",
255 | "isFollowing": true,
256 | "counts": {
257 | "postCount": 2,
258 | "followerCount": 1,
259 | "followingCount": 0
260 | }
261 | }
262 | ```
263 |
264 |
265 |
266 | Profile - All Posts by Author
267 |
268 | **GET** `http://localhost:8080/profile/barksalot/posts`
269 |
270 | ### Output
271 |
272 | Will return an array with posts by the requested user.
273 |
274 | ```
275 | [
276 | {
277 | "_id": "6122b3ea996cb5001293d2c7",
278 | "title": "Being a Dog is Great!!!!!",
279 | "body": "Woof woof",
280 | "createdDate": "2021-08-22T20:30:34.387Z",
281 | "author": {
282 | "username": "barksalot",
283 | "avatar": "https://gravatar.com/avatar/b9216295c1e3931655bae6574ac0e4c2?s=128"
284 | },
285 | "isVisitorOwner": false
286 | },
287 | {
288 | "_id": "6122b229996cb5001293d2c4",
289 | "title": "My New Post Today",
290 | "body": "Lorem ipsum!",
291 | "createdDate": "2021-08-22T20:23:05.653Z",
292 | "author": {
293 | "username": "barksalot",
294 | "avatar": "https://gravatar.com/avatar/b9216295c1e3931655bae6574ac0e4c2?s=128"
295 | },
296 | "isVisitorOwner": false
297 | }
298 | ]
299 | ```
300 |
301 |
302 |
303 | Profile - List Followers For a User
304 |
305 | **GET** `http://localhost:8080/profile/barksalot/followers`
306 |
307 | ### Output
308 |
309 | Will return an array of the users who follow this user.
310 |
311 | ```
312 | [
313 | {
314 | "username": "brad",
315 | "avatar": "https://gravatar.com/avatar/b9408a09298632b5151200f3449434ef?s=128"
316 | },
317 | {
318 | "username": "meowsalot",
319 | "avatar": "https://gravatar.com/avatar/basdfasfadsf32b5151200f3449434ef?s=128"
320 | }
321 | ]
322 | ```
323 |
324 |
325 |
326 | Profile - List The Users This User Follows
327 |
328 | **GET** `http://localhost:8080/profile/brad/following`
329 |
330 | ### Output
331 |
332 | Will return an array of the users who this user follows.
333 |
334 | ```
335 | [
336 | {
337 | "username": "barksalot",
338 | "avatar": "https://gravatar.com/avatar/b9216295c1e3931655bae6574ac0e4c2?s=128"
339 | },
340 | {
341 | "username": "meowsalot",
342 | "avatar": "https://gravatar.com/avatar/basdfasfadsf32b5151200f3449434ef?s=128"
343 | }
344 | ]
345 | ```
346 |
347 |
348 |
349 | Search
350 |
351 | **POST** `http://localhost:8080/search`
352 |
353 | ### Input
354 |
355 | {"searchTerm": "dog"}
356 |
357 | ### Output
358 |
359 | Returns an array of posts that include the term you searched for.
360 |
361 | ```
362 | [
363 | {
364 | "_id": "6122b3ea996cb5001293d2c7",
365 | "title": "Being a Dog is Great!!!!!",
366 | "body": "Woof woof",
367 | "createdDate": "2021-08-22T20:30:34.387Z",
368 | "author": {
369 | "username": "barksalot",
370 | "avatar": "https://gravatar.com/avatar/b9216295c1e3931655bae6574ac0e4c2?s=128"
371 | },
372 | "isVisitorOwner": false
373 | }
374 | ]
375 | ```
376 |
377 |
378 |
379 | ######
380 |
381 | # Go Practice and Create!
382 |
383 | You will run into many challenges along the way, but you now have a real project to sink your teeth into. With enough Googling, I am confident you can program your own front-end user-interface / proxy server that communicates with this API to create an enjoyable experience for hypothetical users.
384 |
385 | Maybe you want to create a single page application using React or Vue.js. Or maybe you want to have traditional full-page reload / submits using a proxy server powered by PHP or Python. Maybe you want to create an iOS or Android app that communicates with this API. The choice is up to you!
386 |
--------------------------------------------------------------------------------