This project is a part of "The Complete 2021 Web Development Bootcamp" by The London App Brewery, instructed by Dr. Angela Yu and hosted through Udemy. In this project I have created my own REST API to access to a collection of articles from a locally hosted database.
- I have used all the HTTP Request Verbs (GET, POST, PUT, PATCH and DELETE)
- I have implemented the specific patterns of routes/ endpoint URLs in accordance with the standard guidelines of RESTful Routing.
- There is no front end interface of this project
- Used
mongod
command from a terminal to start a local database server from localhost:27017 - Used
mongosh
command from another terminal to access to the local database with MongoDB shell. - Created a sample database named WikiDB, using the command
use WikiDB
- Created a collection named 'articles' with 4 sample documents, using the command
db.articles.insertMany({})
- Created a dedicated directory for the project and inside that directory initialised NPM using
npm init -y
command from another terminal. - Used
npm install express body-parser ejs mongoose
command to install all the required dependencies for the project. - Started the server with the following boiler plate codebase:
// Requiring necessary NPM modules:
const express = require("express");
const bodyParser = require("body-parser");
const ejs = require("ejs");
const mongoose = require('mongoose');
// Set port for deployement as well as localhost:
const port = process.env.PORT || 3000;
// Initial Setup for the App:
const app = express();
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static("public"));
// Connect to a new MongoDB Database, using Mongoose ODM:
// Create a new collection to store the items:
// Handle HTTP Requests
// Enable client to listen to the appropriate port:
app.listen(port, () => {
console.log(`Server started on port ${port}`);
});
- Modified the server code to connect to our WikiDB database and the articles collection inside it:
// Connect to a new MongoDB Database, using Mongoose ODM:
mongoose.connect('mongodb://localhost:27017/wikiDB');
// Create a new collection to store the articles:
const articleSchema = new mongoose.Schema ({
title: {
type: String,
required: true
},
content: String
})
const Article = mongoose.model('Article', articleSchema);
- Handled HTTP
GET
,POST
andDELETE
requests made on the/articles
route:
app.route('/articles')
.get( (req, res) => {
Article.find({}, (err, articles) => {
if(!err) {
res.send(articles);
} else {
res.send(err);
}
})
})
.post( (req, res) => {
const title = req.body.title;
const content = req.body.content;
const article = new Article({title, content});
article.save(err => {
if(!err) {
res.send("Successfully added a new article.");
} else {
res.send(err);
}
})
})
.delete( (req, res) => {
Article.deleteMany({}, err => {
if(err) {
res.send(err);
} else {
res.send("Successfully deleted all articles");
}
})
});
- Handled HTTP
GET
,PUT
,PATCH
andDELETE
requests made on the/articles/:customURL
route:
app.route('/articles/:customURL')
.get( (req,res) => {
Article.findOne({ title: req.params.customURL }, (err, article) => {
if(!err) {
if(article) {
res.send(article);
} else {
res.send("No record found");
}
} else {
res.send(err);
}
})
})
.put((req, res) => {
Article.replaceOne(
{title: req.params.customURL},
{title: req.body.title, content: req.body.content},
(err) => {
if(!err) {
res.send("Successfully replaced the selected article.")
} else {
res.send(err);
}
}
)
})
.patch((req, res) => {
Article.updateOne(
{title: req.params.customURL},
req.body,
(err, result) => {
if(!err) {
res.send("Successfully updated the selected article.")
} else {
res.send(err);
}
}
)
})
.delete( (req, res) => {
Article.deleteOne({title: req.params.customURL}, err => {
if(err) {
res.send(err);
} else {
res.send("Successfully deleted the selected article");
}
})
});