javascript - 在本地服务器上的 node.js 中读取和解析 csv

标签 javascript node.js csv

我正在尝试从我的 Web 应用程序(以及在浏览器中 - 用于测试目的)打开我存储在本地 Node 服务器上的 csv 文件。

它是一个快速服务器,但是当我尝试通过绝对路径在浏览器中访问/打开文件时,我得到“无法获取文件路径错误”。当路径正确时,我不确定为什么我无法获取文件。

文件路径如下所示 http://localhost:8000/files/7e911083-d12c-e5f9-10d7-db8e5e955c51.csv 并且我的服务器已开启。

如何在浏览器中查看 csvs?更不用说从网络应用程序访问了。谢谢

最佳答案

快速说明将文件放入 Node ,然后将其解析为json。


示例 CSV 文件:../THEPATHINYOURPROJECT/TOTHE/csv_FILE_YOU_WANT_TO_LOAD.csv

ABC, 123, Fudge
532, CWE, ICECREAM
8023, POOP, DOGS
441, CHEESE, CARMEL
221, ABC, HOUSE

<强>1。使用以下命令安装 CSV Node 模块:

npm install csv 

<强>2。然后在你的app.js中添加如下代码(注释只是说明功能)

var csv = require('csv'); 
// loads the csv module referenced above.


var obj = csv(); 
// gets the csv module to access the required functionality


function MyCSV(Fone, Ftwo, Fthree) {
    this.FieldOne = Fone;
    this.FieldTwo = Ftwo;
    this.FieldThree = Fthree;
}; 
// Define the MyCSV object with parameterized constructor, this will be used for storing the data read from the csv into an array of MyCSV. You will need to define each field as shown above.


var MyData = []; 
// MyData array will contain the data from the CSV file and it will be sent to the clients request over HTTP. 


obj.from.path('../THEPATHINYOURPROJECT/TOTHE/csv_FILE_YOU_WANT_TO_LOAD.csv').to.array(function (data) {
    for (var index = 0; index < data.length; index++) {
        MyData.push(new MyCSV(data[index][0], data[index][1], data[index][2]));
    }
    console.log(MyData);
});
//Reads the CSV file from the path you specify, and the data is stored in the array we specified using callback function.  This function iterates through an array and each line from the CSV file will be pushed as a record to another array called MyData , and logs the data into the console to ensure it worked.


var http = require('http');
//Load the http module.


var server = http.createServer(function (req, resp) {
    resp.writeHead(200, { 'content-type': 'application/json' });
    resp.end(JSON.stringify(MyData));
});
// Create a webserver with a request listener callback.  This will write the response header with the content type as json, and end the response by sending the MyData array in JSON format.

server.listen(8080);
// Tells the webserver to listen on port 8080(obviously this may be whatever port you want.)

<强>3。在我们创建这个 app.js 文件后打开一个控制台,然后输入以下命令

Node app
  1. 这将显示以下结果

    [  MYCSV { Fone: 'ABC', Ftwo: '123', Fthree: 'Fudge' },
       MYCSV { Fone: '532', Ftwo: 'CWE', Fthree: 'ICECREAM' },
       MYCSV { Fone: '8023', Ftwo: 'POOP, Fthree: 'DOGS' },
       MYCSV { Fone: '441', Ftwo: 'CHEESE', Fthree: 'CARMEL' },
       MYCSV { Fone: '221', Ftwo: 'ABC', Fthree: 'HOUSE' }, ]

<强>5。现在,打开您的网络浏览器并在地址栏中输入以下 URL:http://127.0.0.1:8080 您应该会在浏览器中看到以 JSON 格式显示的结果。

希望对您有所帮助。


如果您的应用完全无法访问该文件,我会首先仔细检查您的权限,确保它在项目内部并位于指定的位置。

关于javascript - 在本地服务器上的 node.js 中读取和解析 csv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42127767/

相关文章:

javascript - 自定义 Twitter oAuth 登录页面

javascript - 如何有条件地使用 JSON 数据生成动态 Angular 形式

R - 从 csv 到 xts 的股市数据

python - 拆分和编辑 CSV 列并按字母顺序排列

javascript - 随光标移动的工具提示

javascript - Sendgrid Node js 不发送电子邮件。程序没有错误信息

node.js - mongojs 实现新的bie

javascript - 在从查找返回之前等待 mongo 写入

python - 转储字典后 csv 文件中缺少值

javascript - 滚动后如何执行功能(jquery)