├── .gitignore ├── README.md ├── package.json ├── LICENSE └── app.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # wiki-API 2 | ### Node.js 3 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "wiki-api", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "app.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "arjun", 10 | "license": "ISC", 11 | "dependencies": { 12 | "ejs": "^3.1.9", 13 | "express": "^4.18.2", 14 | "mongoose": "^7.1.0" 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Arjun C Vinod 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | const express=require("express") 2 | const mongoose=require("mongoose") 3 | const ejs=require("ejs") 4 | const bodyParser = require("body-parser") 5 | const app=express() 6 | 7 | app.set("view-engine","ejs") 8 | 9 | app.use( 10 | bodyParser.urlencoded({ 11 | extended: true, 12 | }) 13 | ) 14 | 15 | app.use(express.static("public")); 16 | 17 | mongoose.connect("mongodb://127.0.0.1/wikiDB",{useNewUrlParser:true}) 18 | 19 | const ArticleSchema={ 20 | title:String, 21 | content:String 22 | } 23 | const Article = mongoose.model("Article",ArticleSchema) 24 | 25 | app.route("/articles") 26 | .get(async(req,res)=>{ 27 | const articles=await Article.find({}) 28 | res.send(articles) 29 | }) 30 | .post((req,res)=>{ 31 | const newAricle= Article({ 32 | title:req.body.title, 33 | content:req.body.content 34 | }) 35 | newAricle.save().then(res.send("successfully added")) 36 | 37 | }) 38 | .delete((req,res)=>{ 39 | Article.deleteMany().then( res.send("Deleted Everything")) 40 | }) 41 | 42 | app.route("/articles/:articlename") 43 | .get(async(req,res)=>{ 44 | const results=await Article.findOne({title:req.params.articlename}) 45 | if(results){ 46 | res.send(results) 47 | }else{ 48 | res.send("Not found") 49 | } 50 | 51 | }) 52 | .put((req,res)=>{ 53 | Article.findOneAndUpdate( 54 | {title:req.params.articlename}, 55 | {title:req.body.title,content:req.body.content} 56 | ).then( res.send("updated")) 57 | 58 | }) 59 | .delete((req,res)=>{ 60 | Article.deleteOne({title:req.params.articlename}).then(res.send("deleted")) 61 | }) 62 | .patch((req,res)=>{ 63 | Article.findOneAndUpdate({title:req.params.title},{$set:req.body}).then(res.send("updated")) 64 | }) 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | app.listen(3000,()=>{ 73 | console.log("server started"); 74 | }) 75 | //h --------------------------------------------------------------------------------