从node.js和Typescript中的动态文件名加载JSON的最佳方法是什么?
我正在使用node.js和Typescript创建一个简单的Web应用程序来构建我对它的熟悉程度,并且我想知道加载其运行时确定其文件名的JSON配置文件的最佳方法。
我在网上找到了一些建议:
- Using the fs module in node
- Importing the JSON file like a module
第一种方法似乎应该有效,但第二种看起来更整洁。问题是,使用第二种方法,我有以下错误:
An import declaration can only be used in a namespace or module. ts(1232)
因为我想在构造函数中导入文件,文件名指定为参数。其次,如果我尝试将给定的文件名附加到我希望从中获取的常量目录,则会出现以下错误:
';' expected. ts(1005)
这是我正在尝试加载JSON的类中的(非编译)代码片段,以及我正在尝试加载的示例JSON文件。
Typescript类:
import Floor from './Floor'
import Elevator from './Elevator'
import Person from './Person'
class Building {
public name: string
private floors: Floor[]
private elevators: Elevator[]
private people: Person[]
private time: number
constructor(config_filename: string) {
import * as config from '../config/'+config_filename
const building = (<any>config).building
this.name = name
this.floors = []
building.floors.forEach((floor) => {
this.floors.push(new Floor(floor.number, floor.name))
})
this.elevators = []
building.elevators.forEach((elevator) => {
this.elevators.push(new Elevator(elevator.name, elevator.weight_capacity, elevator.start_floor_no, this))
})
this.people = []
building.people.forEach((person) => {
const person_instance = new Person(person.name, 10, person.algorithm)
this.people.push(person_instance)
this.floors[person.start_floor_no].addOccupant(person_instance)
})
this.time = 0
}
...
示例JSON配置文件:
{
"building": {
"name": "Basic Inc.",
"floors": [
{
"number": 0,
"name": "Ground Floor"
},
{
"number": 1,
"name": "1st Floor"
}
],
"elevators": [
{
"name": "Bruce",
"weight_capacity": 100,
"start_floor_no": 0
}
],
"people": [
{
"name": "Wendy Fox",
"start_floor_no": 0,
"algorithm": "desk"
}
]
}
}
我应该坚持使用第一种方法,还是有一些更简洁的方法来加载JSON文件,其文件名仅在运行时已知?
回答如下:在您的示例中,import
语句适用于TS被“编译”到JS中,而不是在运行时使用new Building(<name>)
(当代码实际执行时,在Node过程编译之后)。
选项1是为Node执行此操作的好方法。我想你正试图摆脱尴尬的fs功能。
另一种选择是使用代码中的GET请求本身,虽然它表面上是相同的,但您可以轻松地探索有点整洁的async / await
函数*。
*(几个月前的最后一次我使用了fs,但我没有尝试使用async / await,但现在可能已经改变了吗?)
从node.js和Typescript中的动态文件名加载JSON的最佳方法是什么?
我正在使用node.js和Typescript创建一个简单的Web应用程序来构建我对它的熟悉程度,并且我想知道加载其运行时确定其文件名的JSON配置文件的最佳方法。
我在网上找到了一些建议:
- Using the fs module in node
- Importing the JSON file like a module
第一种方法似乎应该有效,但第二种看起来更整洁。问题是,使用第二种方法,我有以下错误:
An import declaration can only be used in a namespace or module. ts(1232)
因为我想在构造函数中导入文件,文件名指定为参数。其次,如果我尝试将给定的文件名附加到我希望从中获取的常量目录,则会出现以下错误:
';' expected. ts(1005)
这是我正在尝试加载JSON的类中的(非编译)代码片段,以及我正在尝试加载的示例JSON文件。
Typescript类:
import Floor from './Floor'
import Elevator from './Elevator'
import Person from './Person'
class Building {
public name: string
private floors: Floor[]
private elevators: Elevator[]
private people: Person[]
private time: number
constructor(config_filename: string) {
import * as config from '../config/'+config_filename
const building = (<any>config).building
this.name = name
this.floors = []
building.floors.forEach((floor) => {
this.floors.push(new Floor(floor.number, floor.name))
})
this.elevators = []
building.elevators.forEach((elevator) => {
this.elevators.push(new Elevator(elevator.name, elevator.weight_capacity, elevator.start_floor_no, this))
})
this.people = []
building.people.forEach((person) => {
const person_instance = new Person(person.name, 10, person.algorithm)
this.people.push(person_instance)
this.floors[person.start_floor_no].addOccupant(person_instance)
})
this.time = 0
}
...
示例JSON配置文件:
{
"building": {
"name": "Basic Inc.",
"floors": [
{
"number": 0,
"name": "Ground Floor"
},
{
"number": 1,
"name": "1st Floor"
}
],
"elevators": [
{
"name": "Bruce",
"weight_capacity": 100,
"start_floor_no": 0
}
],
"people": [
{
"name": "Wendy Fox",
"start_floor_no": 0,
"algorithm": "desk"
}
]
}
}
我应该坚持使用第一种方法,还是有一些更简洁的方法来加载JSON文件,其文件名仅在运行时已知?
回答如下:在您的示例中,import
语句适用于TS被“编译”到JS中,而不是在运行时使用new Building(<name>)
(当代码实际执行时,在Node过程编译之后)。
选项1是为Node执行此操作的好方法。我想你正试图摆脱尴尬的fs功能。
另一种选择是使用代码中的GET请求本身,虽然它表面上是相同的,但您可以轻松地探索有点整洁的async / await
函数*。
*(几个月前的最后一次我使用了fs,但我没有尝试使用async / await,但现在可能已经改变了吗?)