BlogAPI is a Node.js Application utilizing Express, Mongoose, and MongoDB to demonstrate a RESTful API. DotEnv and a .env file is also included to show the use of a .env file containing environment variables. No important information is stored here such as AWS Keys, Passwords, or any other secrets.
These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. This is not intended to run on a production system.
To use and run BlogAPI you must have Node.js installed, npm (included with Node.js) or yarn package manager. MongoDB is also required, installation instructions can be found here.
To begin using BlogAPI clone the repo locally using the command below:
HTTPS:
git clone https://github.com/robbyandrews/blogapi.git
SSH:
git@github.com:robbyandrews/blogapi.git
You can also download the zip file here. If you choose the zip file method, extract the archive.
In the folder you have cloned or where you have extracted your zip file, install your dependencies.
Node:
npm i
Yarn:
yarn install
Your dependencies are now installed and you are ready to begin using the application.
We recommend using nodemon
to run BlogAPI, nodemon
will restart the node service when changes are made and saved. Nodemon can be installed globally with npm.
npm i -g nodemon
Start the application
If nodemon
is installed:
npm run devStart
or
nodemon server.js
BlogAPI utilizes Express routes and MongoDB to store and retrieve data. Currently the Model utilized by mongoose and MongoDB to store blog data is the following:
author:{
type: String,
required: true
},
content:{
type: String,
required: true
},
postDate:{
type: Date,
required: true,
default: Date.now
}
Currently there are the following routes, all served with Content-Type: application/json
which means that a JSON object should always be the expected result, even when an error occurs:
GET All Blogs in the Database:
GET /blogs
This will return all of the objects in the database. If the database is empty and no objects exist, then an empty array will be sent.
GET Blog by id:
GET /blogs/:id
All objects stored in the database are given an id as an attribute. By utilizing the id in the URL you can retrieve a given object specified by its id attribute.
Create a blog entry using POST:
POST /blogs
Content-Type: application/json
{
"author":"John Doe",
"content": "This is my first post"
}
To create a blog entry using the POST method, the body must contain a JSON object with the required keys of author: string
and content: string
. Two other key value pairs will be generated automatically: the id value which is created by MongoDB to identify documents and a postDate which is the value of the JavaScript function Date.now
.
Update a blog entry by id using PATCH
PATCH /blogs/:id
Content-Type: application/json
{
"author":"Updated Name",
"content": "Updated Content"
}
Using the PATCH
method along with a JSON body and the id of the desired post, you can update the post with a new author, and/or new content. A PATCH
method was chosen over PUT
as the PUT
method will replace the entire URI request, and PATCH
will only replace the chosen items.
Delete a blog entry by id using DELETE
DELETE /blogs/:id
A post may be deleted using the DELETE
method and its corresponding id.
Authentication, API Limits, adding more routes, expand the schema to allow for more attributes in the documents. Add routes to find posts by an author, or even search content.