如何在项目中设置文件的路径?

时间:2019-04-27 10:41:12

标签: json reactjs meteor markdown

如果保留完整路径,则一切正常。但这无法正常工作,因为它应该在其他计算机上运行。

我尝试写路径:

const jsonData = JSON.parse(fs.readFileSync('/app/data/faqQuestions', { encoding: 'utf8' }));

控制台中的问题

Error: ENOENT: no such file or directory, open 'C:\app\data\faqQuestions.json'

如果您在之前删除斜杠:app/data/faqQuestions.json

Error: ENOENT: no such file or directory, open 'C:\Users\mi\AppData\Local\Temp\meteor-test-runqxi9h2.08bd.meteor\local\build\programs\server\app\data\faqQuestions.json'

必须规定正确的路径才能在任何计算机上工作。 我需要PWD之类的东西。

3 个答案:

答案 0 :(得分:1)

您可以使用节点中的require File.expand_path('../boot', __FILE__) require 'rails/all' # Require the gems listed in Gemfile, including any gems # you've limited to :test, :development, or :production. Bundler.require(*Rails.groups) module Proj class Application < Rails::Application # Settings in config/environments/* take precedence over those specified here. # Application configuration should go into files in config/initializers # -- all .rb files in that directory are automatically loaded. # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone. # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC. # config.time_zone = 'Central Time (US & Canada)' # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded. # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s] # config.i18n.default_locale = :de # Do not swallow errors in after_commit/after_rollback callbacks. config.active_record.raise_in_transactional_callbacks = true end end 模块在​​文件系统中获取正确的路径:

path

答案 1 :(得分:0)

欢迎使用堆栈溢出。您不应该像这样直接访问文件系统。这样做有两个原因:

1)位置因计算机而异 2)在生产中部署到docker容器时,本地文件系统是只读的,除非您为此目的专门安装了卷 3)构建Meteor时,它运行的包位于.meteor / local ...中的某个位置,因此您不能真正使用pwd

将文件存储在外部存储中(例如S3存储桶,请参见ostrio:files了解更多信息),或将它们作为对象放入Mongo数据库中更有意义。

如果仍然确定要从文件系统访问文件,则可以在Meteor.settings中指定一个位置,这意味着您可以为正在运行的每台服务器/计算机独立设置该位置。

答案 2 :(得分:0)

您可以放置​​文件,例如在您的应用程序源的“私有”目录中,例如

./ private / data / faq.json

要获取该内容,您可以使用:

const jsonData = JSON.parse(fs.readFileSync(files.pathJoin(dataPath, 'faqQuestions'), { encoding: 'utf8' }));

之后,应该可以在服务器上加载json

fun MaterialButton.setRightIcon() { 
    TextViewCompat.setCompoundDrawablesRelative(this, null, null, this.icon, null) 
}

我在流星组件中使用了它,以与位于Github(https://github.com/4commerce-technologies-AG/meteor-package-env-settings)的ENV配置文件一起使用

欢呼