Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Angela committed Dec 14, 2018
0 parents commit f10936f
Show file tree
Hide file tree
Showing 1,952 changed files with 256,024 additions and 0 deletions.
130 changes: 130 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
//jshint esversion:6

const express = require("express");
const bodyParser = require("body-parser");
const ejs = require("ejs");
const mongoose = require('mongoose');

const app = express();

app.set('view engine', 'ejs');

app.use(bodyParser.urlencoded({
extended: true
}));
app.use(express.static("public"));

mongoose.connect("mongodb://localhost:27017/wikiDB", {
useNewUrlParser: true
});

const articleSchema = {
title: String,
content: String
};

const Article = mongoose.model("Article", articleSchema);

app.route("/articles")


.get(function(req, res){
Article.find(function(err, articles){
if (articles) {
const jsonArticles = JSON.stringify(articles);
res.send(jsonArticles);
} else {
res.send("No articles currently in wikiDB.");
}
});
})

.post(function(req, res){
const newArticle = Article({
title: req.body.title,
content: req.body.content
});

newArticle.save(function(err){
if (!err){
res.send("Successfully added a new article.");
} else {
res.send(err);
}
});
})

.delete(function(req, res){

Article.deleteMany(function(err){
if (!err){
res.send("Successfully deleted all the articles in wikiDB.");
} else {
res.send(err);
}
});

});


app.route("articles/:articleTitle")

.get(function(req, res){
const articleTitle = req.params.articleTitle;
Article.findOne({title: articleTitle}, function(err, article){
if (article){
const jsonArticle = JSON.stringify(article);
res.send(jsonArticle);
} else {
res.send("No article with that title found.");
}
});
})

.patch(function(req, res){
const articleTitle = req.params.articleTitle;
Article.findOneAndUpdate(
{title: articleTitle},
{content: req.body.newContent},
function(err){
if (!err){
res.send("Successfully updated selected article.");
} else {
res.send(err);
}
});
})

.put(function(req, res){

const articleTitle = req.params.articleTitle;

Article.findOneAndUpdate(
{title: articleTitle},
{content: req.body.newContent},
{overwrite: true},
function(err){
if (!err){
res.send("Successfully updated the content of the selected article.");
} else {
res.send(err);
}
});
})


.delete(function(req, res){
const articleTitle = req.params.articleTitle;
LostPet.findOneAndDelete({title: articleTitle}, function(err){
if (!err){
res.send("Successfully deleted selected article.");
} else {
res.send(err);
}
});
});


app.listen(3000, function() {
console.log("Server started on port 3000");
});
1 change: 1 addition & 0 deletions node_modules/.bin/mime

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions node_modules/.bin/semver

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

224 changes: 224 additions & 0 deletions node_modules/accepts/HISTORY.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions node_modules/accepts/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit f10936f

Please sign in to comment.