이미지 업로드 - GET
localhost:3000/item/image
router.get('/image', async function(req, res, next) {
try {
const no = Number(req.query.no);
const dbConn = await db.connect(DBURL);
const coll = dbConn.db(DBNAME).collection(ITEMCOLL);
const result = await coll.findOne(
{_id:no}, // 조건
{projection : { filedata:1, filetype:1}} // 필요한 항목만 가져오기
);
console.log(result);
res.contentType(result.filetype); // 원래 타입 json -> image/jpeg
return res.send(result.filedata.buffer);
}
catch (err) {
console.error(err);
return res.send({status:-1, result : err});
}
});
상품목록 - GET
localhost:3000/item/select
router.get('/select', async function(req, res, next) {
try {
// 페이지 정보가 전달
const page = Number(req.query.page);
// 1 -> skip(0) -> skip( (page-1) * 10 )
// 2 -> skip(10) -> skip( (page-1) * 10 )
// 3 -> skip(20) -> skip( (page-1) * 10 )
const dbConn = await db.connect(DBURL);
const coll = dbConn.db(DBNAME).collection(ITEMCOLL);
// console.log(req);
// 물품코드, 물품명, 가격, 수량, 등록일
const result = await coll.find(
{},
{projection : { _id:1, name:1, price:1, quantity:1}}
)
.sort({_id:-1}) // 1 : 오름차순 / -1 : 내림차순
.skip( (page-1) * 10 ) // 건너 뛸 개수
.limit(10) // 10개 까지만
.toArray(); // 배열로 변환
// find() : 여러개 가져오기 => toArray() 배열로 변환(마지막에 배열화)
console.log(result);
// 페이지네이션에서 사용 할 전체 게시물 수
const total = await coll.countDocuments({});
return res.send({status:200, result : result, total : total});
}
catch (err) {
console.error(err);
return res.send({status:-1, result : err});
}
});