문서 › 함수와 Node
Node에서 쓰기
서버나 스크립트에서 Buffer를 그대로 넣어 검사하고 행을 꺼냅니다.
82excel 진입점은 DOM을 쓰지 않으므로 Node에서도 그대로 import 됩니다. fs.readFile 로 읽은 Buffer 는 Uint8Array 이므로 바로 넣으면 됩니다. 파일 경로 문자열은 받지 않습니다.
import { readFile } from 'node:fs/promises';
import { check, readRows } from '82excel';
const bytes = await readFile('./customers.xlsx'); // Buffer 는 Uint8Array 입니다
const result = await check(bytes, { headers: ['이름', '전화번호', '금액'], rows: { min: 1 } });
console.log(result.ok, result.errors); // true []
const { rows } = await readRows(bytes);
console.log(rows[0]); // { 이름: '홍길동', 전화번호: '010-1234-5678', 금액: 5000 }
const wrong = await check(bytes, { headers: ['A', 'B'] });
console.log(wrong.errors.map((e) => `${e.code}: ${e.message}`));
// [ 'COLUMN_COUNT: Expected 2 columns, found 3',
// 'HEADER_MISMATCH: Column A: expected "A", found "이름"',
// 'HEADER_MISMATCH: Column B: expected "B", found "전화번호"' ]업로드 서버에서 다시 검사하기
브라우저의 <excel-82> 가 통과시킨 파일이라도 서버에서 한 번 더 검사하세요. 같은 규칙으로 같은 결과가 나옵니다.
// 업로드를 받는 서버에서 다시 검사하기 (예: Express + multer)
import '82excel/locales/ko';
import { check } from '82excel';
app.post('/api/upload', upload.single('file'), async (req, res) => {
const r = await check(req.file.buffer, { headers: ['이름', '전화번호', '금액'], locale: 'ko' });
if (!r.ok) return res.status(422).json(r.errors);
res.send('ok');
});옵션과 한도는 함수 API에 있습니다.