쇼핑몰 구현 프로젝트

[쇼핑몰 구현 프로젝트] 04. Backend, Database 틀잡기

binning 2024. 6. 22. 23:15

Controller의 자세한 내용들은 이후 게시물들에 있다.

파일 구성

 

① DB model

const userSchema = new Schema({
  name: { type: String, required: true },
  email: { type: String, required: true, unique: true },
  password: { type: String, required: true, minlength: 6 },
  address: { type: String, required: true },
  phonenumber: { type: String, required: true },
  admin: { type: Boolean, required: true },
  cart: [
    { type: mongoose.Types.ObjectId, ref: 'Product' },
  ],
  history: [
    { type: mongoose.Types.ObjectId, ref: 'Product' },
  ]
});
const productSchema = new Schema({
  name: { type: String, required: true },
  description: { type: String, required: true },
  image: { type: String, required: true },
  category: { type: String, required: true },
  price: { type: Number, required: true },
  stock: { type: Number, required: true },
});
const reviewSchema = new Schema({
  user: { type: mongoose.Types.ObjectId, required: true, ref: 'User' },
  product: { type: mongoose.Types.ObjectId, required: true, ref: 'Product' },
  comment: { type: String, required: true },
  star: { type: Number, required: true }
});

 

② Routes

router.get('/:uid', usersControllers.getUser);

router.post(
  '/signup',
  [
    check('name').not().isEmpty(),
    check('email').normalizeEmail().isEmail(),
    check('password').isLength({ min: 6 }),
    check('repeatpassword').isLength({ min: 6 }),
    check('address').not().isEmpty(),
    check('phonenumber').not().isEmpty()
  ],
  usersControllers.signup
);

router.patch(
  '/:uid',
  [
    check('name').not().isEmpty(),
    check('address').not().isEmpty(),
    check('phonenumber').not().isEmpty()
  ],
  usersControllers.editUser
);

router.post('/login', usersControllers.login);

router.post('/:uid', usersControllers.payment);
router.get('/', productsControllers.getHomeProduct);

router.get('/:pid', productsControllers.getProduct);

router.get('/category/:cid', productsControllers.getCategory);

router.post(
  '/',
  fileUpload.single('image'),
  [
    check('name').not().isEmpty(),
    check('description').not().isEmpty(),
    //check('image').not().isEmpty(),
    check('category').not().isEmpty(),
    check('price').not().isEmpty(),
    check('stock').not().isEmpty()
  ],
  productsControllers.createProduct
);

router.patch(
  '/:pid',
  [
    check('name').not().isEmpty(), 
    check('description').not().isEmpty(),
    check('category').not().isEmpty(),
    check('price').not().isEmpty(),
    check('stock').not().isEmpty()
  ],
  productsControllers.editProduct
);

router.delete('/:pid', productsControllers.deleteProduct);
router.get('/product/:pid', reviewsControllers.getProductReviews);

router.get('/user/:uid', reviewsControllers.getUserReviews);

router.post(
  '/',
  [
    check('star').not().isEmpty(),
    check('comment').not().isEmpty(),
    check('userId').not().isEmpty(),
    check('productId').not().isEmpty()
  ],
  reviewsControllers.createReview
);

router.delete('/:rid', reviewsControllers.deleteReview);
router.post('/:uid/:pid', cartsControllers.putInCart);

router.delete('/:uid/:pid', cartsControllers.deleteFromCart);

router.delete('/:uid', cartsControllers.deleteAllFromCart);

 

③ app.js

Error(CORS, 잘못된 경로...) 다루기

app.use((req, res, next) => {
  res.setHeader('Access-Control-Allow-Origin', '*');
  res.setHeader(
    'Access-Control-Allow-Headers',
    'Origin, X-Requested-With, Content-Type, Accept, Authorization'
  );
  res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PATCH, DELETE, PUT, OPTIONS');
  next();
});

app.use((req, res, next) => {
  const error = new Error('Could not find this route.');
  error.code = 404;
  throw error;
});

app.use((error, req, res, next) => {
  if (res.headerSent) {
    return next(error);
  }
  res.status(error.code || 500);
  res.json({ message: error.message || 'An unkown error occured!' });
});