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
if(error.response){
errorMsg=error.response.data
if (error.response) {
errorMsg = error.response.data
console.log(errorMsg)
}else if(error.request){
errorMsg=error.request
} else if (error.request) {
errorMsg = error.request
console.log(errorMsg)
}else{
errorMsg=error.message
} else {
errorMsg = error.message
console.log(errorMsg)
}
displayError(errorMsg)
......
import express from 'express';
// import bodyParser from "body-parser";
import fs from 'fs';
import connectDb from './schemas/index.js'
import userRouter from "./routes/user.routes.js";
import productRouter from './routes/product.routes.js';
......@@ -10,14 +10,17 @@ import config from './config.js'
import authRouter from './routes/auth.routes.js'
import cors from 'cors'
fs.readdir('uploads', (error) => {
if (error) {
fs.mkdirSync('uploads');
}
})
connectDb()
const app = express();
app.use(express.json());
app.use(cors())
app.use(express.static(path.join(process.cwd(), 'dist')))
// app.use(bodyParser.urlencoded({ extended: true }))
......
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) => {
console.log('req.body=', req.body)
const { pro_name, price, stock, main_category, sub_category, description, main_image, detail_image } = req.body
try {
const newProduct = await new Product ({
pro_name, price, stock, main_category, sub_category, description, main_image, detail_image
const { pro_name, price, stock, main_category, sub_category, description } = req.body
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()
res.json(newProduct)
} catch (error) {
console.log(error)
res.status(500).send('죄송합니다. 다시 입력해 주십시오.')
res.status(500).send('제품 정보 등록에 실패하였습니다. 다시 진행해 주십시오.')
}
}
export default { regist }
\ No newline at end of file
export default { imageUpload, regist }
\ No newline at end of file
import User from "../schemas/User.js";
import isLength from 'validator/lib/isLength.js'
import bcrypt from 'bcryptjs'
import isLength from 'validator/lib/isLength.js';
import bcrypt from 'bcryptjs';
const signup = async (req, res) => {
console.log(req.body)
......@@ -14,7 +14,6 @@ const signup = async (req, res) => {
return res.status(422).send(`${id}가 이미 사용중입니다.`)
}
const hash=await bcrypt.hash(password,10)
const newUser = await new User ({
......@@ -34,5 +33,4 @@ const signup = async (req, res) => {
}
}
export default { signup }
\ No newline at end of file
import express from "express";
import path from 'path';
import multer from 'multer';
import productCtrl from '../controllers/product.controller.js';
import fs from 'fs'
// process.cwd() + '/client/public/image'
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)
})
router.route('/regist')
.post(productCtrl.imageUpload, productCtrl.regist)
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('/productone')
// .get(productCtrl.getProduct)
router.route('/regist')
.post(productCtrl.regist)
// upload.array('main_image'),
export default router
\ No newline at end of file
import mongoose from 'mongoose'
const { String} = mongoose.Schema.Types
const { String, Number } = mongoose.Schema.Types
const ProductSchema = new mongoose.Schema({
pro_name: {
......@@ -20,11 +20,11 @@ const ProductSchema = new mongoose.Schema({
required: true,
default: 0
},
size: {
sizes: {
type: Array,
required: true
},
color: {
colors: {
type: Array,
required: true
},
......@@ -33,20 +33,19 @@ const ProductSchema = new mongoose.Schema({
required: true,
},
sub_category: {
type: Array,
type: [String],
required: true,
},
description: {
type: String,
required: true,
},
main_image: {
main_imgUrl: {
type: String,
required: true,
required: true
},
detail_image: {
type: String,
required: true,
detail_imgUrls: {
type: [String]
}
}, {
timestamps: true
......
......@@ -16,7 +16,6 @@ const UserSchema = new mongoose.Schema({
type: String,
required: true,
},
number1: {
type: String,
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