2017-10-30 2 views
0

js-xlsx을 사용하여 Node.js 서버에서 Excel 파일을 읽습니다. 파일은 다중 부분 양식 데이터를 사용하여 게시되며 임시 위치에 저장됩니다. 그러나 파일을 읽는 동안 내 Node.js 스레드가 차단되어 다른 요청을 처리 할 수없는 것처럼 보입니다. 다음 코드에서 /에 대한 호출이 보류 중일 때 /를 호출하면 /가 호출 될 때까지 호출이 완료 될 때까지 기다립니다.Node.js에서 js-xlsx를 사용하여 비 차단 읽기

const express = require('express'); 
    var XLSX = require('xlsx'); 

    const app = express() 

    app.get('/', function (req, res) { 
     res.send('Hello World!') 
    }) 

    app.get('/read', function (req, res) { 
     console.log("Starting file read ..."); 
     var workbook = XLSX.readFile("/tmp/myfile.xlsx") 
     console.log("Done file read ..."); 
     res.send('File Read') 
    }) 

    app.get('/readwithtimeout', function (req, res) { 
     console.log("Starting file read ..."); 
     setTimeout(function() { 
     var workbook = XLSX.readFile("/tmp/myfile.xlsx") 
     console.log("Done file read ..."); 
     }, 0); 
     res.send('File Read') 
    }) 

    app.listen(3000, function() { 
     console.log('Example app listening on port 3000!') 
    }) 

Excel 파일을 읽고 구문 분석하는 올바른 방법입니까? 아니면 다른 일을해야합니까? 여기에 자신의 문서에 따르면

답변

0

: https://github.com/SheetJS/js-xlsx/tree/master/demos/server

에서 ReadFile/인 WriteFile 함수는 FS 포장 FileSync를 {, 읽기, 쓰기}. 꽤 성가신

을 .... XLS 구문 분석도 느리고 동기 경우에 도움이 될 경우

const XLSX = require('xlsx'); 
const fs = require('fs') 

fs.readFile("./sample.xls", (err, data) => { 
    if (err) { 
     console.log(err) 
     return; 
    } 
    const workbook = XLSX.read(data, {type:'buffer'}) 
    console.log("Done file read ..."); 

}) 

console.log("end script") 

잘 모르겠어요 : 당신이 대신 XLSX.read()을 사용할 수 있습니다 당신은 같은 뭔가 fs.readFile() 경우 비동기 버전을 사용 할 수있는 것 같습니다 적어도 읽은 파일은 비동기입니다.

+0

그 중 하나가 도움이되지 않는다고 생각합니다. 이제 브라우저에서 파일을 구문 분석하고 Node.js 서버의 구문 분석 된 데이터를 다시 보내 데이터베이스에 대량로드를 수행하는 것이 적절한 해결책이라고 생각합니다. –