Saiddit: A Reddit Clone RESTful API Backend

Using NODE.js, Express.js and MongoDB!

Routes

The below are the available routes on this api.


GET

/api/topics

Returns all of the topics available


/api/topics/:topic/articles

Return all the articles for a certain topic


/api/articles

Returns all the articles.


/api/articles/:articleID

If you want to access an individual article you can include the article ID and it will return a single article using the params articleID.


/api/articles/:article_id/comments

Get all the comments for a individual article


/api/users/:username

Returns a JSON object with the profile data for the specified user.


POST

/api/articles/:article_id/comments

Add a new comment to an article. This route requires a JSON body with a comment key and value pair e.g: {"comment": "This is my new comment"}


PUT

/api/articles/:article_id

Increment or Decrement the votes of an article by one. This route requires a vote query of 'up' or 'down' e.g: https://vast-tundra-92428.herokuapp.com/api/articles/:article_id?vote=up


/api/comments/:comment_id

Increment or Decrement the votes of a comment by one. This route requires a vote query of 'up' or 'down' e.g: https://vast-tundra-92428.herokuapp.com/api/comments/:comment_id?vote=down


DELETE

/api/comments/:comment_id

Deletes a comment if the comment was created by the default user, 'Northcoder'. Currently this is the only user.


Aim:

As part of creating a reddit clone this RESTful API uses Node JS, Express JS and MongoDB. The project stack is entirely Javascript allowing fantastic levels of continuity between the front-end and the back-end. This then feeds into a React JS front-end hosted at https://sleepy-atoll-80436.herokuapp.com/. The database has been populated with seed data.


Setup

This application was built in Node JS. Express JS provides all the API routes fetching data from the MongoDB database hosted on mLabs. Should you wish the current output could be connected to your own front-end. Using 'fetch' or the Axios package you can make requests for the data this API provides.


Versions

Node.js : 8.3.0
Express.js : 4.14.0
Mongoose": 4.7.0
Body-parser: 1.15.2
Cors: 2.8.4


Implementation

After seeding the database locally it was connected to an Express server. The Express router is then imported to manage the various endpoints. In order to use MongoDB the database is connected to in the server.js file. The index.js loads in the express server from the server.js file with all it's configurations defined. Initially the server.js connected directly to the database but later i added in a config file.

In order to carry data i installed body-parser and used it within the express server app to parse request bodies into json. I added the routes to a file apiRouter.js using the express.Router(). Then i created a controller.js file to manage all the handling of the requests and responses. This includes the management of calls to the MongoDB. I tested all the routes using either Chrome and Postman. I made show all the data responses were consistent in format, normally sending an object with the 'name' of that response as the response object's only key, and the data itself being the value associated with that key. Obviously, there were a few exceptions to this rule. And some routes do not return a specific response to be handled.

Once i had all the routes working as desired and i had added CORS (Cross-origin resource sharing) i connected a front-end. Once i was sure this was working i created a Heroku project and pushed this project. After a few tweaks to the configuration the project was live.

Finally i added this static page you see now using express.static to designate a point of call for static resources.


Learning

Heroku has it's own project configuration variables that can be added to a project. I Initially pushed with my local setup and caused a lot of confusion about what process.env it was loading from.

This is a simple example of the setup of a RESTful API.