SHARE
TWEET

Untitled

a guest Aug 18th, 2019 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const express = require('express');
  2. const app = express();
  3. const mongoose = require('mongoose');
  4. const slug = require('mongoose-slug-generator');
  5. const bodyParser = require('body-parser');
  6. const cors = require('cors');
  7. require('dotenv/config');
  8.  
  9. mongoose.plugin(slug);
  10.  
  11. // middleware
  12. app.use(cors());
  13. app.use(bodyParser.json());
  14.  
  15. // routes
  16. const postRoute = require('./routes/posts');
  17. const siteRoute = require('./routes/sites');
  18.  
  19. app.use('/posts', postRoute);
  20. app.use('/sites', siteRoute);
  21.      
  22. const express = require('express');
  23. const router = express.Router();
  24. const Post = require('../models/Post');
  25.  
  26. // get all posts
  27. router.get('/', async (req, res) => {
  28.   try {
  29.     const posts = await Post.find();
  30.     res.json(posts);
  31.   } catch(err) {
  32.     res.json({message: err})
  33.   }
  34. });
  35.  
  36.  
  37. // create blog post
  38. router.post('/add', async (req, res) => {
  39.   const post = new Post({
  40.     title: req.body.title,
  41.     slug: req.body.slug,
  42.     author: req.body.author,
  43.     creation: req.body.creation,
  44.     body: req.body.body,
  45.     css: req.body.css,
  46.     enabled: req.body.enabled
  47.   })
  48.  
  49.   try {
  50.     const savedPost = await post.save()
  51.     res.json(savedPost);
  52.   } catch(err) {
  53.     res.json({message: err})
  54.   }
  55. });
  56.  
  57.  
  58. // get specific post
  59. router.get('/:post', async (req, res) => {
  60.   try {
  61.     const post = await Post.findById(req.params.postId)
  62.     res.json(post)
  63.   } catch(err) {
  64.     res.json({message: err})
  65.   }
  66. });
  67.  
  68.  
  69. // delete specific post
  70. router.delete('/:post', async (req, res) => {
  71.   try {
  72.     const removePost = await Post.deleteOne({ _id: req.params.postId })
  73.     res.json(removePost)
  74.   } catch(err) {
  75.     res.json({message: err})
  76.   }
  77. });
  78.  
  79.  
  80. // delete specific post
  81. router.patch('/:post', async (req, res) => {
  82.   try {
  83.     const updatedPost = await Post.updateOne({ _id: req.params.postId }, { $set: {
  84.       title: req.params.title
  85.     }})
  86.     res.json(updatedPost)
  87.   } catch(err) {
  88.     res.json({message: err})
  89.   }
  90. });
  91.  
  92. module.exports = router;
  93.      
  94. [
  95.   {
  96.     "ID": "some unique ID",
  97.     "name": "Example Blog Site 01",
  98.     "enabled": true,
  99.     "blogs": [
  100.       {
  101.         "title": "My blog title",
  102.         "slug": "slug",
  103.         "description": "some blog content"
  104.       },
  105.       {
  106.         "title": "My blog title",
  107.         "slug": "slug",
  108.         "description": "some blog content"
  109.       }
  110.     ]
  111.   },
  112.   {
  113.     "ID": "some unique ID",
  114.     "name": "Example Blog Site 02",
  115.     "enabled": true,
  116.     "blogs": [
  117.       {
  118.         "title": "My blog title",
  119.         "slug": "slug",
  120.         "description": "some blog content"
  121.       },
  122.       {
  123.         "title": "My blog title",
  124.         "slug": "slug",
  125.         "description": "some blog content"
  126.       }
  127.     ]
  128.   }
  129. ]
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top