-
Notifications
You must be signed in to change notification settings - Fork 268
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Angela
committed
Dec 14, 2018
0 parents
commit f10936f
Showing
1,952 changed files
with
256,024 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.