简体   繁体   English

Node.js和React.js应用程序之间的差异

[英]Differences between Node.js and React.js applications

I have a section of code in javascript that looks like this: 我在javascript中有一段代码看起来像这样:

var fs = require('fs');

var subscriptions = []

var array = fs.readFileSync('optimal.csv', 'utf8').toString().split("\r");
for (i in array) {
    stock = array[i].split(':')[0];
    if (stock)
        subscriptions.push(stock)
}

When I say: 当我说:

node .\realtime.js

it has no problem reading the file 'optional.csv'. 读取文件“ optional.csv”没有问题。

However, if I copy the same section of code to a react.js application built with create-react-app and try to run it, I get errors: 但是,如果我将同一段代码复制到使用create-react-app构建的react.js应用create-react-app并尝试运行它,则会出现错误:

TypeError: fs.readFileSync is not a function

when I run it thus: 当我这样运行时:

npm run start

var fs = require('fs');

// Grid data as an array of arrays
const quoteList = [];
var i = 0;
var stock;

var array = fs.readFileSync('optimal.csv').toString().split("\r");
    for (i in array) {
        stock = array[i].split(':')[0];
        if (stock)
            quoteList.push(stock)
    }
  • What is the correct way to read a .csv file from a purely react application? 从纯React应用程序读取.csv文件的正确方法是什么?

fs is a module implemented by node.js which is not provided by javascript itself. fs是由node.js实现的模块,而javascript本身未提供。 Maybe other environments provide fs as well, but react is just a module like fs , rather than another environment, so it can not provide extra module 也许其他环境也提供了fs ,但是react只是像fs这样的模块,而不是其他环境,因此它不能提供额外的模块

Another thing, react code run in browser, where you expect fs refer to? 另一件事, react代码在浏览器中运行,您希望fs指向哪里? Read every user's file system? 读取每个用户的文件系统?

If you want to read your local csv file. 如果您想读取本地的csv文件。 You can use some bundler like Webpack which can bundle csv file into your output Javascript file. 您可以使用诸如Webpack之类的捆绑程序,该捆绑程序可以将csv文件捆绑到您的输出Javascript文件中。 If reading local csv is what you want, you can do some thing like 如果您要阅读本地的 csv,则可以执行以下操作

const csv =require('/path/to/csv')

Maybe you need some plugin, because create-react-app may not provide the csv loader 也许您需要一些插件,因为create-react-app可能不提供csv加载器

Here are the steps to get a json from the csv file 这是从csv文件获取json的步骤

  1. npm run eject in the project root npm run eject在项目根目录中npm run eject
  2. npm install --save dsv-loader
  3. open webpack.config.dev.js under config config下打开webpack.config.dev.js
  4. under module>loaders>exclude(line 110) add /\\.csv$/ in the list module>loaders>exclude(line 110) ,在列表中添加/\\.csv$/
  5. add {test: /\\.csv$/,loader: 'dsv-loader'} entry in module>loaders module>loaders {test: /\\.csv$/,loader: 'dsv-loader'}添加{test: /\\.csv$/,loader: 'dsv-loader'}条目
  6. re-run npm start 重新运行npm start
  7. require('path/to/csv') will return json require('path/to/csv')将返回json
  8. do the same thing on webpack.config.prod.js if you need the production bundle 如果需要生产包,请在webpack.config.prod.js上执行相同的操作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM