product.controller.js 1.07 KB
Newer Older
Jiwon Yoon's avatar
Jiwon Yoon committed
1
import Product from "../schemas/Product.js";
Jiwon Yoon's avatar
0111    
Jiwon Yoon committed
2
3
4
5
6
7
8
9
import multer from 'multer';


const upload = multer({ dest: 'uploads/'});

const fileUpload = upload.fields([
    { name: 'main_image', maxCount: 1 },
    { name: 'detail_image', maxCount: 1 }
Jiwon Yoon's avatar
Jiwon Yoon committed
10
]) 
Jiwon Yoon's avatar
Jiwon Yoon committed
11
12
13

const regist = async (req, res) => {
    console.log('req.body=', req.body)
Jiwon Yoon's avatar
0111    
Jiwon Yoon committed
14
15
16
17
18
19
    const { pro_name, price, stock, main_category, sub_category, description } = req.body
    console.log(req.files)
    const main_image = req.files['main_image'][0].filename
    console.log(main_image)
    const detail_image = req.files['detail_image'][0].filename
    // const { main_image, detail_image} = req.files
Jiwon Yoon's avatar
Jiwon Yoon committed
20
21
22
23
24
25
26
27
28
29
30
    try {
        const newProduct = await new Product ({
            pro_name, price, stock, main_category, sub_category, description, main_image, detail_image
        }).save()
        res.json(newProduct)
    } catch (error) {
        console.log(error)
        res.status(500).send('죄송합니다. 다시 입력해 주십시오.')
    }
}

Jiwon Yoon's avatar
Jiwon Yoon committed
31
32
33
34
35
36

const getProduct = (req, res) => {
    res.json(req.body)
}

export default { regist, fileUpload , getProduct}