Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Building the CREATE Post Endpoint | Building REST APIs with Node.js and Express.js
Backend Development with Node.js and Express.js

book
Building the CREATE Post Endpoint

We will examine creating a new post using the "CREATE POST" route within the postsRoutes.js file. This route is responsible for handling the creation of a post and saving it to the data source (database/posts.json).

Route Definition

The code below defines the "CREATE POST" route using router.post():

js
router.post("/", validatePostData, async (req, res, next) => { ... }
  • This route is configured to handle HTTP POST requests to the root path / ;

  • It utilizes the validatePostData middleware, which ensures that the data sent in the request body is valid before proceeding.

Data Validation Middleware

Before delving into the route's logic, we must create a data validation middleware. This middleware ensures that the data sent in the request body is valid before attempting to create a new post.

js
// Middleware to validate post data
function validatePostData(req, res, next) {
const { username, postTitle, postContent } = req.body;

if (!username || !postTitle || !postContent) {
return res.status(400).json({ error: "Missing required fields" });
}

// If data is valid, proceed to the next route handler
next();
}
  • The validatePostData function extracts the username , postTitle , and postContent from the request body;

  • It checks whether these fields are present. If any of them are missing ( !username , !postTitle , or !postContent ), it responds with a 400 (Bad Request) status code and an error message indicating missing required fields;

  • If the data is valid, the middleware calls next() , allowing the request to continue to the route handler (the "CREATE POST" route in this case).

With the data validation middleware in place, let's continue with the "CREATE POST" route logic.

New Post Data

To create a new post, we generate a unique ID using Date.now().toString(). Additionally, we extract the username, postTitle, and postContent from the request body.

js
const newPost = {
id: Date.now().toString(),
username: req.body.username,
postTitle: req.body.postTitle,
postContent: req.body.postContent,
};

Add the New Post to the Database

The following steps detail how the new post is added to the database:

js
const data = await readData();
data.push(newPost);
await fs.writeFile("./database/posts.json", JSON.stringify(data));
  • The existing data is read from the JSON file using the asynchronous readData function, as previously explained;

  • The new post is added to the data array;

  • The updated data array is then written back to the JSON file to save the newly created post.

Sending a Response

Upon successful creation of the new post, a success response is sent to the client. The response includes a status code of 201 (Created) and the details of the newly created post in JSON format.

js
res.status(201).json(newPost);

Error Handling

We wrap the route code in a try-catch block to handle potential errors during data retrieval or request processing. Any errors that occur are logged to the console for debugging purposes:

js
try {
// ... (code for retrieving and processing data)
} catch (error) {
console.error(error.message);
}

Complete code of the postsRoutes.js file at this step

js
const express = require("express");
const fs = require("fs/promises");
const validatePostData = require("../middlewares/validateData");

const router = express.Router();

// Function to read data from the JSON file
async function readData() {
try {
// Read the contents of the `posts.json` file
const data = await fs.readFile("./database/posts.json");
// Parse the JSON data into a JavaScript object
return JSON.parse(data);
} catch (error) {
// If an error occurs during reading or parsing, throw the error
throw error;
}
}

// GET ALL POSTS
router.get("/", async (req, res, next) => {
try {
// Call the `readData` function to retrieve the list of posts
const data = await readData();
// Send the retrieved data as the response
res.status(200).send(data);
} catch (error) {
// If an error occurs during data retrieval or sending the response
console.error(error.message); // Log the error to the console for debugging
}
});

// GET POST BY ID
router.get("/post/:id", async (req, res, next) => {
try {
// Extract the post ID from the request parameters

Complete code of the validateData.js file

js
// Middleware to validate post data
function validatePostData(req, res, next) {
const { username, postTitle, postContent } = req.body;

if (!username || !postTitle || !postContent) {
return res.status(400).json({ error: "Missing required fields" });
}

// If data is valid, proceed to the next route handler
next();
}

module.exports = validatePostData;

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 4. Kapittel 7

Spør AI

expand
ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

some-alt