I'm currently developing an Express REST API tailored for a dynamic blog management dashboard. I plan to utilize Vue JS as the Front-End, allowing users to authenticate, establish websites, and publish blog posts on specific sites they have created. All data will be stored in Mongo DB Atlas.
Following a foundational guide on constructing a restful api, encompassing CRUD operations and integrating with Mongo DB, has been immensely helpful so far.
The guide demonstrates how to define models, incorporate routes using middleware, and execute GET and POST requests to communicate with the database effectively.
It illustrates a method for incorporating blog posts as individual objects into an array in Mongo DB. The next step is to expand this functionality to group arrays of objects (blog posts) and assign them to specific parent objects while retaining CRUD functionalities for each post.
Presently, my app.js
file loads sites and posts as follows:
const express = require('express');
const app = express();
const mongoose = require('mongoose');
const slug = require('mongoose-slug-generator');
const bodyParser = require('body-parser');
const cors = require('cors');
require('dotenv/config');
mongoose.plugin(slug);
// Middlewares
app.use(cors());
app.use(bodyParser.json());
// Routes
const postRoute = require('./routes/posts');
const siteRoute = require('./routes/sites');
app.use('/posts', postRoute);
app.use('/sites', siteRoute);
Each route manages the CRUD functions for adding an individual blog post or site successfully.
While straightforward thus far, I am encountering challenges understanding what modifications are required to link a particular blog post to a designated collection object in the database.
The JavaScript file containing routes to add a blog post looks like this:
const express = require('express');
const router = express.Router();
const Post = require('../models/Post');
// Get all posts
router.get('/', async (req, res) => {
try {
const posts = await Post.find();
res.json(posts);
} catch(err) {
res.json({message: err})
}
});
...
module.exports = router;
While this setup functions flawlessly, my ultimate objective is to enable users to POST a specific blog post to a unique object created through my sites model.
Ultimately, I aim to structure the data like this:
[
{
"ID": "some unique ID",
"name": "Example Blog Site 01",
"enabled": true,
"blogs": [
{
"title": "My blog title",
"slug": "slug",
"description": "some blog content"
},
{
"title": "My blog title",
"slug": "slug",
"description": "some blog content"
}
]
},
...
]
Each object's blogs array should correspond to my sites.js
, serving as the parent object represented by my sites.js
route.
This approach empowers users to swiftly generate sets of blog posts under specific endpoints, for example:
- https://example.com/api/sites/my_site/posts - Retrieve all posts
https://example.com/api/sites/my_site_02/posts - Retrieve all posts
https://example.com/api/sites/my_site/posts/blog-post-01 - Obtain specific post
- https://example.com/api/sites/my_site_02/posts/blog-post-01 - Obtain specific post