-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddpost.js
More file actions
25 lines (21 loc) · 781 Bytes
/
addpost.js
File metadata and controls
25 lines (21 loc) · 781 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
const express = require('express');
const router = express.Router();
const db = require('./db.js');
router.post('/', (req, res) => {
const {writer_id, title, category, study_name, content} = req.body;
const sql = `
INSERT INTO posts (writer_id, title, category, study_name, content)
VALUES (?, ?, ?, ?, ?)
`;
const values = [writer_id, title, category, study_name, content];
db.query(sql, values, (err, results) => {
if (err) {
console.error('Error querying database: ' + err.stack);
res.status(500).send('Internal Server Error');
return;
}
const postId = results.insertId; // 삽입된 레코드의 post_id 값
res.status(201).json({ postId: postId, message: 'Post added successfully' })
});
});
module.exports = router;