Hello Dev Community! ๐
It is officially Day 50 โ a massive half-century milestone on my daily, unbroken streak toward mastering full-stack MERN engineering! Reaching Day 50 feels absolutely incredible. Yesterday, I mapped out dynamic path parameters. Today, I wired the input engine by building a complete asset workflow: Capturing Host "Add New Product" data payloads and committing them to local file storage pipelines!
Following Prashant Sir's backend sequence, today was all about bridging the gap between host client forms and backend architecture using the Model-View-Controller framework.
๐ง Key Learnings From Day 50 (Product Ingestion & Storage)
Processing data mutations sent from input forms requires tight coordination between parsing middlewares and file serialization engines. Here is how I structured the logic today:
1. Intercepting Form Submissions (POST /host/add-product)
Set up a clean route mapping inside hostRouter.js to process dynamic data blocks sent by the host. The endpoint parses input parameters securely via backend streams.
2. Utilizing Class Instances for Storage
Instead of directly pushing raw unstructured dictionaries into file records, I initialized a new object instance using my Day 48 structural class framework (new houseList(...)). This forces incoming data attributesโlike name, price, location, and imagesโto match my exact system layout blueprint.
3. Asynchronous File Serialization
Invoked the instance method .save(), which runs a non-blocking background task: it reads the active database layout array inside homesdata.json, appends the newly formulated object safely, and flushes the stringified update back onto the hard drive array using Node's fs operations.
javascript
// A conceptual look at how my controller hands data over to the model layer today
const Product = require("../model/home");
exports.postAddProduct = (req, res) => {
const { title, price, location, rating, imageUrl } = req.body;
// Instantiating the core class data mold
const newProduct = new Product(title, price, location, rating, imageUrl);
// Triggering the async file storage engine
newProduct.save();
// Smoothly redirecting back to user collection boards after successful write operations
res.redirect("/");
};
Top comments (0)