Commit 6a24c10c authored by Yoon, Daeki's avatar Yoon, Daeki 😅
Browse files

파일 업로드 로직 작성

parent 74ee10b0
import { NextFunction, Request, Response } from "express";
import formidable from "formidable";
import { isEmpty } from "../helpers";
import { asyncWrap } from "../helpers/asyncWrap";
interface TypedRequest extends Request {
auth: any;
user: any;
files: any;
}
export const fileUpload = asyncWrap(async (req, res, next) => {
const typedReq = req as TypedRequest;
const form = formidable();
await new Promise((resolve, reject) => {
form.parse(req, (err, fields, files) => {
if (err) {
reject(err);
return;
}
console.log("fields", fields);
console.log("files", files);
typedReq.body = fields;
if (isEmpty(files)) {
typedReq.files = null;
} else {
typedReq.files = files;
}
resolve(files);
});
});
next();
return;
});
export * as authCtrl from "./auth.controller"; export * as authCtrl from "./auth.controller";
export * as fileCtrl from "./file.controller";
export * as questionCtrl from "./question.controller"; export * as questionCtrl from "./question.controller";
export * as surveyCtrl from "./survey.controller"; export * as surveyCtrl from "./survey.controller";
export * as roleCtrl from "./role.controller"; export * as roleCtrl from "./role.controller";
......
...@@ -3,9 +3,18 @@ import { userDb } from "../db"; ...@@ -3,9 +3,18 @@ import { userDb } from "../db";
import { asyncWrap } from "../helpers/asyncWrap"; import { asyncWrap } from "../helpers/asyncWrap";
import { TypedRequestAuth } from "./auth.controller"; import { TypedRequestAuth } from "./auth.controller";
export const createUser = asyncWrap(async (req, res) => { interface TypedRequest extends Request {
auth: any;
user: any;
files: any;
}
export const createUser = asyncWrap(async (reqExp, res) => {
const req = reqExp as TypedRequest;
const user = req.body; const user = req.body;
console.log("user body", user); console.log("user body", user);
console.log("files ", req.files);
const newUser = await userDb.createUser(user); const newUser = await userDb.createUser(user);
return res.json(newUser); return res.json(newUser);
}); });
......
export { asyncWrap } from "./asyncWrap"; export { asyncWrap } from "./asyncWrap";
export const isEmpty = (obj: any) => {
return (
obj && // 👈 null and undefined check
Object.keys(obj).length === 0 &&
Object.getPrototypeOf(obj) === Object.prototype
);
};
import { model, Schema } from "mongoose";
interface IFile {
name: string;
path: string;
}
const schema = new Schema<IFile>(
{
name: { type: String },
path: { type: String },
},
{ timestamps: true, toJSON: { versionKey: false } }
);
export default model<IFile>("File", schema);
export { default as File } from "./file.model";
export { default as Question, IQuestion } from "./question.model"; export { default as Question, IQuestion } from "./question.model";
export { default as Survey, ISurvey } from "./survey.model";
export { default as Role } from "./role.model"; export { default as Role } from "./role.model";
export { default as Survey, ISurvey } from "./survey.model";
export { default as User, IUser } from "./user.model"; export { default as User, IUser } from "./user.model";
...@@ -5,6 +5,7 @@ export interface IUser { ...@@ -5,6 +5,7 @@ export interface IUser {
name?: string; name?: string;
password: string; password: string;
role?: Types.ObjectId; role?: Types.ObjectId;
avatar?: Types.ObjectId;
} }
const validateEmail = (email: string) => { const validateEmail = (email: string) => {
...@@ -23,6 +24,7 @@ const schema = new Schema<IUser>( ...@@ -23,6 +24,7 @@ const schema = new Schema<IUser>(
name: { type: String }, name: { type: String },
password: { type: String, required: true, select: false }, password: { type: String, required: true, select: false },
role: { type: Schema.Types.ObjectId, ref: "Role" }, role: { type: Schema.Types.ObjectId, ref: "Role" },
avatar: { type: Schema.Types.ObjectId, ref: "File" },
}, },
{ {
toJSON: { toJSON: {
......
import express from "express"; import express from "express";
import { userCtrl, authCtrl } from "../controllers"; import { userCtrl, authCtrl, fileCtrl } from "../controllers";
const router = express.Router(); const router = express.Router();
router router
.route("/") .route("/")
.get(authCtrl.requireLogin, userCtrl.getUsers) .get(authCtrl.requireLogin, userCtrl.getUsers)
.post(authCtrl.requireLogin, userCtrl.createUser); .post(authCtrl.requireLogin, fileCtrl.fileUpload, userCtrl.createUser);
router router
.route("/:userId") .route("/:userId")
......
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