validatePostInput 미들웨어는 Express.js 애플리케이션에서 요청 데이터의 유효성을 검사하기 위해 사용됩니다.
이 미들웨어는 주로 API 엔드포인트에 대한 요청이 올바른 형식과 내용을 가지고 있는지 확인하는 역할을 합니다.
// middleware/validatePostInput.js
const validatePostInput = (req, res, next) => {
const { username, title, content, post_date } = req.body;
// Check if any of the required fields are missing
if (!username || !title || !content || !post_date) {
// If any field is missing, respond with a 400 Bad Request status and an error message
return res.status(400).json({ error: 'All fields are required' });
}
// If all fields are present, proceed to the next middleware or route handler
next();
};
module.exports = validatePostInput;
// community.js
const validatePostInput = require('./middleware/validatePostInput'); // 추가해서 적용
...