Backend API for a news site supporting users, articles and comments.
- Getting Started
- — Prerequisites
- — Installation
- Testing
- Deployment
- Available Scripts
- API Reference
- Built With
- Authors
- Acknowledgements
These instructions will get you a copy of the project running on your local machine with a set of development and testing data. This is for testing and development only; for production see deployment
The project requires you to have PostgreSQL installed. It also assumes that npm
is available.
Clone this repository:
git clone https://github.com/Jakub-L/nc-news-api.git
Install the package dependencies:
npm install
Generate a knexfile.js
with database configuration (this may require adding your postgres credentials):
knex init -x nc-news-api
Initialise the databases:
npm run setup-dbs
Seed the database with development data:
npm run seed
Start the development server:
npm run dev
The API can now be reached at localhost on port 9090 (this can be changed in listen.js
):
// GET localhost:9090/api/articles/1
{
"article": {
"author": "jessjelly",
"title": "Running a Node App",
"article_id": 1,
"body": "This is part two of a series on how to get up and running with Systemd and Node.js. This part dives deeper into how to successfully run your app with systemd long-term, and how to set it up in a production environment.",
"topic": "coding",
"created_at": "2016-08-18T12:07:52.389Z",
"votes": 0,
"comment_count": "8"
}
}
In order to run the automated tests, first ensure the databases have been initialised (migrations and seeding are done automatically before each test):
npm run setup-dbs
Afterwards, the tests can be run for the entire package:
npm test
Or alternatively, only the utility functions can be tested by running:
npm run test-utils
The API is hosted on Heroku.
npm run setup-dbs
- create test and development databases locally,npm run migrate-make
- create a new migration file for Knex.js,npm run migrate-latest
- run all migrations,npm run migrate-latest:prod
- run all migrations (using Heroku configuration),npm run migrate-rollback
- rollback all migrations,npm run migrate-rollback:prod
- rollback all migrations (using Heroku configuration),npm test
- run full testing suite,npm run test-utils
- run testing suite for utility functions,npm run seed
- rollback, migrate latest, then seed tables with development data,npm run seed:prod
- rollback, migrate latest, then seed production tables (using Heroku configuration)npm run dev
- run server withnodemon
for hot reload,npm start
- run server withnode
Returns JSON object with all available endpoints.
Returns array with all article topic objects.
Adds topic to table of topics and returns the newly added object.
Body Params:
slug
(type:string
): unique identifier of topicdescription
(type:string
): description of the topic
Returns array with all article objects.
Queries:
author
(type:string
): username of authortopic
(type:string
): topic of articlesort_by
(type:string
): name of column by which to sort returned data. Default:created_at
order
(type:string
): order of sorting, can beasc
for ascending ordesc
for descending. Default:desc
p
(type:int
): page of results to return. Default:1
limit
(type:int
): number of entries per page. Default10
Adds article to table of articles and returns the newly added object.
Body Params:
author
(type:string
): username of authortitle
(type:string
): article titlebody
(type:string
): article contenttopic
(type:string
): slug of topic of article
Returns article object matching article ID.
Path Params:
article_id
(type:int
): ID of article to return
Changes the number of votes for an article with matching article ID.
Path Params:
article_id
(type:int
): ID of article to return
Body Params:
inc_votes
(type:int
): number of votes to be added to article. Can be negative, for decrementing votes.
Removes an article with matching article ID.
Retrieves all comments for an article with matching article ID.
Path Params:
article_id
(type:int
): ID of article to return
Queries:
sort_by
(type:string
): name of column by which to sort returned data. Default:created_at
order
(type:string
): order of sorting, can beasc
for ascending ordesc
for descending. Default:desc
p
(type:int
): page of results to return. Default:1
limit
(type:int
): number of entries per page. Default10
Adds a comment for an article with matching article ID.
Path Params:
article_id
(type:int
): ID of article to return
Body Params:
username
(type:string
): username of author. Must exists in theusers
table.body
(type:string
): body of the comment to be added
Changes the number of votes for a comment with matching comment ID.
Path Params:
comment_id
(type:int
): ID of comment to return
Body Params:
inc_votes
(type:int
): number of votes to be added to comment. Can be negative, for decrementing votes.
Removes a comment with matching comment ID.
Returns array of all users.
Adds user to users table and returns newly added object
Body Params:
username
(type:string
): unique username identifier for username
(type:string
): user's nameavatar_url
(type:string
): url leading to user's avatar
Retrieves user object with matching username.
Path Params:
username
(type:string
): username of user to return
- PostgreSQL: Relational database used
- node-postgres: Interfacing with PostgreSQL
- Express: Web framework used
- Knex.js: SQL query builder
- Jakub-L: Initial work
- Northcoders: For providing test and development data