Posts
Q&A
hashtag set ๊ตฌํ ์ง๋ฌธ
์ ์ ๊ท์์ด ์ด์ํ๋ค์ ๊ฐ์ฌํฉ๋๋ค
- 0
- 2
- 288
Q&A
router.post ๊ฒฝ๋ก ์ง๋ฌธ
if (confirm('ํ๋ก์ํ์๊ฒ ์ต๋๊น?')) { axios.post(`/user/${userId}/follow`)main.html์ ํ๋ก ํธ์ฝ๋์ userId๊ฐ :id๊น์ง ์ด์ด์ง๋ ๊ฑฐ๊ตฐ์ ๊ฐ์ฌํฉ๋๋ค:id์ ๊ฐ์ req.params.id๋ก ์ฌ์ฉํ ์ ์๊ตฌ์:name์ผ๋ก ๋์ด์๋ค๋ฉด req.params.name์ผ๋ก ์ฌ์ฉํ๋ ๊ฑฐ๊ตฐ์
- 0
- 2
- 175
Q&A
๋ก๊ทธ์์ ์ค๋ฅ
router.get('/logout', isLoggedIn, async(req, res, next) => { req.logout((err) => { req.session.destroy(); if (err) { res.redirect("/"); } else { res.redirect("/"); } }); });๋งํฌ ์ฐธ๊ณ ํด์ ๋ง๋ค์๋๋ฐ ๋งํฌ์ ๋๊ธ ๋ค์ ๋ถ์ ์์ ์ฝ๋ ์ฒ๋ผ ์ธ์์ next๋ฅผ ๋ฃ์ด๋์ผ์ จ๋๋ฐ ํจ์์ next๋ฅผ ๋ฐํํ๋๊ฒ๋ ์๋๋ฐ next๋ฅผ ์ธ์๋ก ๊ฐ์ง ํ์๊ฐ ์๋์?
- 0
- 3
- 228
Q&A
ํธ์ํฐ ๊ฒ์๊ธ ์ ๋ก๋ ๊ธฐ๋ฅ์ ๊ตฌํํ๋๋ฐ ๊ฐ์๊ธฐ ๋ก๊ทธ์ธ์ด ์๋ฉ๋๋ค
router.use((req, res, next) => { res.locals.user = null; res.locals.followerCount = 0; res.locals.followingCount = 0; res.locals.followerIdList = []; next(); });page.js ๋ถ๋ถ์์ res.locals.user๊ฐ null๋ก ๋์ด์์ด์ ๊ทธ๋ฐ๊ฑฐ์์ต๋๋ค.์ ๊ฐ ๊ณ์ ์ค๋ฅ๊ฐ ๋๊ฐ์ง๊ณ ์์ ๊ฐ์๋ฅผ ๋ค์ผ๋ฉด์ ๊ณ์ ์ฝ๋๋ฅผ ์์ ํ๋ค๋ณด๋๊น ์์ ๊ฐ์ ๋ด์ฉ์ ์ฝ๋๊ฐ ์ด์ํ๊ฒ ๋ฐ๊ปด์ ๋ ์ค๋ฅ์ฐพ๊ธฐ ํ๋ค์๋ค์ ๋ต๋ณ๊ฐ์ฌ๋๋ฆฝ๋๋ค!router.use((req, res, next) => { res.locals.user = req.user; res.locals.followerCount = 0; res.locals.followingCount = 0; res.locals.followerIdList = []; next(); });
- 0
- 5
- 312
Q&A
ํธ์ํฐ ๊ฒ์๊ธ ์ ๋ก๋ ๊ธฐ๋ฅ์ ๊ตฌํํ๋๋ฐ ๊ฐ์๊ธฐ ๋ก๊ทธ์ธ์ด ์๋ฉ๋๋ค
(์ฌ์ง)์ด๋ฐ ์๋ฌ๋ฉ์์ง๊ฐ ๋์ค๊ณ ์์ต๋๋ค
- 0
- 5
- 312
Q&A
ํธ์ํฐ ๊ฒ์๊ธ ์ ๋ก๋ ๊ธฐ๋ฅ์ ๊ตฌํํ๋๋ฐ ๊ฐ์๊ธฐ ๋ก๊ทธ์ธ์ด ์๋ฉ๋๋ค
404 ์๋ฌ๊ฐ ๋จ๋ ๊ฒ์ ์ ๊ฐ ํ์ผ์ ์๋ ์ฌ์ง ํ์ผ์ ์ง์ ๋๋ฐ ๋ฐ์ดํฐ ๋ฒ ์ด์ค๋ ์ง์ฐ์ง ์์์ ์๊ธฐ๋ ์ค๋ฅ์ธ๊ฒ ๊ฐ์ต๋๋คํ์ฌ ํ๋ก ํธ์์ ๋ฐ์ดํฐ๋ฉ์ด์ค์ ์ ์ฅ๋ ์ด๋ฉ์ผ๊ณผ ์์ด๋๋ฅผ ์ ๋ ฅ์ ํ๋ฉด ๋ก๊ทธ์ธ ํ์ด์ง๊ฐ ๋์ค์ง ์๊ณ ๋ฐ๋ก ๋ฆฌํ๋ ์ฌ๊ฐ ๋์ด ๋ฒ๋ฆฝ๋๋ค๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ ์ฅ๋์ง ์์ ์๋ฌด ์ด๋ฉ์ผ์ด๋ ๋น๋ฐ๋ฒํธ๋ฅผ ์ ๋ ฅํ์๋๋ ๋์์ผํ๋ "๊ฐ์ ๋์ง ์์ ํ์์ ๋๋ค"๋ผ๋ ๊ฒฝ๊ณ ์ฐฝ์ด ๋จ์ง ์๊ณ ๋ฐ๋ก ๋ฆฌํ๋ ์ฌ๊ฐ ๋์ด๋ฒ๋ฆฝ๋๋ค
- 0
- 5
- 312
Q&A
ํธ์ํฐ ๊ฒ์๊ธ ์ ๋ก๋ ๊ธฐ๋ฅ์ ๊ตฌํํ๋๋ฐ ๊ฐ์๊ธฐ ๋ก๊ทธ์ธ์ด ์๋ฉ๋๋ค
peserve log ์ฒดํฌ๋ฅผ ํ๊ณ ๋๋ ธ๋๋ฐ ๋ณํ๊ฐ ์์ด์ app.js๋ฅผ ์ ๋ก์ด๋ ์ฝ๋๋ก ๋ฐ๊ฟ๋ณด์์ต๋๋ค๊ทธ๋ฌ๋๋ ๋ก๊ทธ๋ 404์๋ฌ๊ฐ ๋ฐ์ํ๋ ๊ฑธ๋ก ๋์ค๊ณ (์ฌ์ง)ํ์ ํ๋ก ํธ์ชฝ์ ์ด๋ฐ์ํ ์ ๋๋ค(์ฌ์ง)
- 0
- 5
- 312
Q&A
notNull Violation: Post.content cannot be null
(์ฌ์ง)content๊ฐ ๋น์ด์๋ค๊ณ ๋์ค์ง๋ง ์ง๊ธ ํ์ธํด ๋ณด๋ ์ฌ์ง์ ์ ๋ก๋ํ ๋ ์ฌ์ง์ ์ด๋ฆ์ด ์ด์ํ๊ฒ ๋์ค๋๊ฒ์ ๋ณด๋ ๋ค๋ฅธ ๊ณณ์์ ์ค๋ฅ๊ฐ ๋๊ฒ์ด content๊น์ง ์ํฅ์ ์ฃผ๋ ๊ฒ ๊ฐ์ต๋๋ค.๊ทธ๋ฆฌ๊ณ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ชฝ์๋ ๊ฐ๋ค์ด ๋ค์ด๊ฐ์ง ์๊ณ ์์ต๋๋ค.๊ทธ๋ฐ๋ฐ ์์ธ์ด ๋ฌด์์ธ์ง ๋ชจ๋ฅด๊ฒ ์ต๋๋คconst express = require('express'); const multer = require('multer'); const path = require('path'); const fs = require('fs'); const { Post, Hashtag } = require('../models'); const { isLoggedIn } = require('./middlewares'); const { nextTick } = require('process'); const router = express.Router(); try { fs.readdirSync('uploads'); } catch (error) { console.error('uploads ํด๋๊ฐ ์์ด uploads ํด๋๋ฅผ ์์ฑํฉ๋๋ค.'); fs.mkdirSync('uploads'); } const upload = multer({ storage: multer.diskStorage({ destination(req, file, cb) { cb(null, 'uploads/'); }, filename(req, file, cb) { const ext = path.extname(file.originalname); cb(null, path.basename(file.originalname, ext) + Date.now() + ext); }, }), limits: { fileSize: 5 * 1024 * 1024 }, }); //์์ฒญ์ img๋ก ํ์ง๋ง ์ค์ ํ์ผ์ upload์ ์๋ค //์ด๋ฅผ ํด๊ฒฐํด์ฃผ๋ ๊ฒ์ด app.js์ express.static //image ๋ผ์ฐํฐ router.post('/img', isLoggedIn, upload.single('img'), (req, res) => { console.log(req.file); res.json({ url: `/img/${req.file.filename}` }); }); //๊ฒ์๊ธ router router.post('/', isLoggedIn, upload.none(), async(req, res, next) => { try { const post = await Post.create({ cotent: req.body.content, img: req.body.url, UserId: req.user.id, }); res.redirect('/'); } catch (error) { console.error(error); next(error); } }); module.exports = router;post.js์ ์ ์ฒด ์ฝ๋์ ๋๋ค
- 0
- 2
- 266