Page 578 - Nodejs 교과서 개정2판
P. 578

console.error(error);
           				next(error);
           		}
           });


           router.get('/join',	isNotLoggedIn,	(req,	res)	=>	{
           		res.render('join',	{
           				title:	'회원가입	-	NodeAuction',
           		});
           });


           router.get('/good',	isLoggedIn,	(req,	res)	=>	{
           		res.render('good',	{	title:	'상품	등록	-	NodeAuction'	});
           });


           try	{
           		fs.readdirSync('uploads');
           }	catch	(error)	{
           		console.error('uploads	폴더가	없어	uploads	폴더를	생성합니다.');
           		fs.mkdirSync('uploads');
           }
           const	upload	=	multer({
           		storage:	multer.diskStorage({
           				destination(req,	file,	cb)	{
           						cb(null,	'uploads/');
           				},
           				filename(req,	file,	cb)	{
           						const	ext	=	path.extname(file.originalname);
           						cb(null,	path.basename(file.originalname,	ext)	+	new	Date().valueOf()	+	ext);
           				},
           		}),
           		limits:	{	fileSize:	5	*	1024	*	1024	},
           });
           router.post('/good',	isLoggedIn,	upload.single('img'),	async	(req,	res,	next)	=>	{
           		try	{
           				const	{	name,	price	}	=	req.body;
           				await	Good.create({
           						OwnerId:	req.user.id,
           						name,
           						img:	req.file.filename,
           						price,
           				});
           				res.redirect('/');
           		}	catch	(error)	{
           				console.error(error);
   573   574   575   576   577   578   579   580   581   582   583