Commit df51efcd authored by 이재연's avatar 이재연
Browse files

Merge remote-tracking branch 'origin/ourMaster' into jaeyeon

parents 98bc3e3a e7e1d3d1
function catchErrors(error,displayError){ function catchErrors(error, displayError) {
let errorMsg let errorMsg
if(error.response){ if (error.response) {
errorMsg=error.response.data errorMsg = error.response.data
console.log(errorMsg) console.log(errorMsg)
}else if(error.request){ } else if (error.request) {
errorMsg=error.request errorMsg = error.request
console.log(errorMsg) console.log(errorMsg)
}else{ } else {
errorMsg=error.message errorMsg = error.message
console.log(errorMsg) console.log(errorMsg)
} }
displayError(errorMsg) displayError(errorMsg)
} }
export default catchErrors export default catchErrors
\ No newline at end of file
import express from 'express'; import express from 'express';
// import bodyParser from "body-parser"; import fs from 'fs';
import connectDb from './schemas/index.js' import connectDb from './schemas/index.js'
import userRouter from "./routes/user.routes.js"; import userRouter from "./routes/user.routes.js";
import productRouter from './routes/product.routes.js'; import productRouter from './routes/product.routes.js';
...@@ -10,14 +10,17 @@ import config from './config.js' ...@@ -10,14 +10,17 @@ import config from './config.js'
import authRouter from './routes/auth.routes.js' import authRouter from './routes/auth.routes.js'
import cors from 'cors' import cors from 'cors'
fs.readdir('uploads', (error) => {
if (error) {
fs.mkdirSync('uploads');
}
})
connectDb() connectDb()
const app = express(); const app = express();
app.use(express.json());
app.use(cors()) app.use(cors())
app.use(express.static(path.join(process.cwd(), 'dist'))) app.use(express.static(path.join(process.cwd(), 'dist')))
// app.use(bodyParser.urlencoded({ extended: true })) // app.use(bodyParser.urlencoded({ extended: true }))
......
import Product from "../schemas/Product.js"; import Product from "../schemas/Product.js";
import multer from 'multer';
const upload = multer({ dest: 'uploads/' })
const imageUpload = upload.fields([
{ name: 'main_image' },
{ name: 'detail_image' }
])
const regist = async (req, res) => { const regist = async (req, res) => {
console.log('req.body=', req.body)
const { pro_name, price, stock, main_category, sub_category, description, main_image, detail_image } = req.body
try { try {
const newProduct = await new Product ({ const { pro_name, price, stock, main_category, sub_category, description } = req.body
pro_name, price, stock, main_category, sub_category, description, main_image, detail_image const main_img = req.files['main_image'][0]
const detail_img = req.files['detail_image']
const main_imgUrl = main_img.filename
const detail_imgUrls = []
detail_img.forEach(file => {
detail_imgUrls.push(file.filename)
})
const newProduct = await new Product({
pro_name, price, stock, main_category, sub_category, description, main_imgUrl, detail_imgUrls
}).save() }).save()
res.json(newProduct) res.json(newProduct)
} catch (error) { } catch (error) {
console.log(error) console.log(error)
res.status(500).send('죄송합니다. 다시 입력해 주십시오.') res.status(500).send('제품 정보 등록에 실패하였습니다. 다시 진행해 주십시오.')
} }
} }
export default { regist } export default { imageUpload, regist }
\ No newline at end of file \ No newline at end of file
import User from "../schemas/User.js"; import User from "../schemas/User.js";
import isLength from 'validator/lib/isLength.js' import isLength from 'validator/lib/isLength.js';
import bcrypt from 'bcryptjs' import bcrypt from 'bcryptjs';
const signup = async (req, res) => { const signup = async (req, res) => {
console.log(req.body) console.log(req.body)
...@@ -13,7 +13,6 @@ const signup = async (req, res) => { ...@@ -13,7 +13,6 @@ const signup = async (req, res) => {
if(user){ if(user){
return res.status(422).send(`${id}가 이미 사용중입니다.`) return res.status(422).send(`${id}가 이미 사용중입니다.`)
} }
const hash=await bcrypt.hash(password,10) const hash=await bcrypt.hash(password,10)
...@@ -34,5 +33,4 @@ const signup = async (req, res) => { ...@@ -34,5 +33,4 @@ const signup = async (req, res) => {
} }
} }
export default { signup }
export default { signup } \ No newline at end of file
import express from "express"; import express from "express";
import path from 'path';
import multer from 'multer';
import productCtrl from '../controllers/product.controller.js'; import productCtrl from '../controllers/product.controller.js';
import fs from 'fs'
// process.cwd() + '/client/public/image'
const router = express.Router() const router = express.Router()
fs.readdir('uploads', (error, data) => {
if (error) {
fs.mkdirSync('uploads');
if (data == undefined) {
fs.mkdirSync('/main')
fs.mkdirSync('/detail')
}
} else console.log("else data",data)
})
const upload = multer({
storage: multer.diskStorage({
destination(req, file, cb) {
cb(null, 'uploads/');
}, // 저장 경로 변경
filename(req, file, cb) {
cb(null, new Date().valueOf() + path.extname(file.originalname));
}, // 파일명 변경
}),
});
router.route('/regist') router.route('/regist')
.post(productCtrl.regist) .post(productCtrl.imageUpload, productCtrl.regist)
// upload.array('main_image'),
// router.route('/productone')
// .get(productCtrl.getProduct)
export default router export default router
\ No newline at end of file
import mongoose from 'mongoose' import mongoose from 'mongoose'
const { String} = mongoose.Schema.Types const { String, Number } = mongoose.Schema.Types
const ProductSchema = new mongoose.Schema({ const ProductSchema = new mongoose.Schema({
pro_name: { pro_name: {
...@@ -20,11 +20,11 @@ const ProductSchema = new mongoose.Schema({ ...@@ -20,11 +20,11 @@ const ProductSchema = new mongoose.Schema({
required: true, required: true,
default: 0 default: 0
}, },
size: { sizes: {
type: Array, type: Array,
required: true required: true
}, },
color: { colors: {
type: Array, type: Array,
required: true required: true
}, },
...@@ -33,20 +33,19 @@ const ProductSchema = new mongoose.Schema({ ...@@ -33,20 +33,19 @@ const ProductSchema = new mongoose.Schema({
required: true, required: true,
}, },
sub_category: { sub_category: {
type: Array, type: [String],
required: true, required: true,
}, },
description: { description: {
type: String, type: String,
required: true, required: true,
}, },
main_image: { main_imgUrl: {
type: String, type: String,
required: true, required: true
}, },
detail_image: { detail_imgUrls: {
type: String, type: [String]
required: true,
} }
}, { }, {
timestamps: true timestamps: true
......
...@@ -16,7 +16,6 @@ const UserSchema = new mongoose.Schema({ ...@@ -16,7 +16,6 @@ const UserSchema = new mongoose.Schema({
type: String, type: String,
required: true, required: true,
}, },
number1: { number1: {
type: String, type: String,
required: true, required: true,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment