Javascript 动态返回配置值(node.js)
我正在构建一个内容中间件,它从外部发布者那里收集内容。发布者将在rss或json中共享其内容,并且键/值字段将彼此不同。为了让事情更简单,我创建了一个配置文件,可以在其中预定义键/值和提要类型。问题是,如何根据发布者名称动态返回此配置值 示例:要获取Publisher#1提要类型,我只需要使用config.articles.rojak_daily.url_提要 我的配置文件/config/index.js 我的主应用程序脚本Javascript 动态返回配置值(node.js),javascript,node.js,config,Javascript,Node.js,Config,我正在构建一个内容中间件,它从外部发布者那里收集内容。发布者将在rss或json中共享其内容,并且键/值字段将彼此不同。为了让事情更简单,我创建了一个配置文件,可以在其中预定义键/值和提要类型。问题是,如何根据发布者名称动态返回此配置值 示例:要获取Publisher#1提要类型,我只需要使用config.articles.rojak_daily.url_提要 我的配置文件/config/index.js 我的主应用程序脚本 const config = require('@config');
const config = require('@config'); // export from config file
class Main {
constructor(){
this.publishers = ['rojak_daily','rojak_weekly'];
}
// Main process
async startExport(){
try{
for(let publisher of this.publishers){
const feedType = await this.getFeedType(publisher)
const result = (feedType == 'rss')? await this.rss.start(publisher): await this.json.start(publisher)
return result
}
}catch(err){
console.log("Error occured: ", err)
}
}
// Get feed type from config
async getFeedType(publisher){
return await config.articles.rojak_daily.url_feed;
// this only return publisher 1 url feed.
// my concern is to dynamically passing variable
// into this config file (example: config.articles.<publisher>.url_feed)
}
}
module.exports = Main
const config=require('@config');//从配置文件导出
班长{
构造函数(){
this.publisher=['rojak_日报','rojak_周刊'];
}
//主要过程
异步startExport(){
试一试{
for(让发布者使用此.publisher){
const feedType=等待此消息。getFeedType(发布者)
const result=(feedType='rss')?等待this.rss.start(publisher):等待this.json.start(publisher)
返回结果
}
}捕捉(错误){
日志(“发生错误:”,err)
}
}
//从配置中获取提要类型
异步getFeedType(发布服务器){
return wait config.articles.rojak_daily.url_feed;
//这仅返回publisher 1 url源。
//我关心的是动态传递变量
//进入这个配置文件(例如:config.articles..url\u提要)
}
}
module.exports=Main
您可以通过使用或与一起使用循环浏览文章,或者由于您已经拥有发布者的名称,您可以使用config.articles[publisher].url\u feed
访问该条目,如下所示:
const-config={
文章:{
罗雅库日报:{//Publisher 1
url:'xxx',
url_提要:“rss”,
id:null,
姓名:'头衔',
Description:'Description',
链接:“链接”,
DatePublishFrom:“pubDate”,
景观图像:“s3image”,
SiteName:“Rojak Daily”,
SiteLogo:空
},
rojak_周刊:{//publisher 2
url:'xxx',
url_提要:“json”,
id:null,
姓名:'姓名',
描述:'Desc',
链接:“链接”,
DatePublishFrom:“pubDate”,
景观图像:“s3image”,
SiteName:“Rojak周刊”,
SiteLogo:空
}
}
}
const publishers=['rojak_daily'、'rojak_weekly']
函数getFeedType(发布者){
返回config.articles[publisher].url\u提要;
}
publisher.forEach(publisher=>console.log(getFeedType(publisher))
您可以通过使用或与一起使用循环浏览文章,或者由于您已经拥有发布者的名称,您可以使用config.articles[publisher].url\u feed
访问该条目,如下所示:
const-config={
文章:{
罗雅库日报:{//Publisher 1
url:'xxx',
url_提要:“rss”,
id:null,
姓名:'头衔',
Description:'Description',
链接:“链接”,
DatePublishFrom:“pubDate”,
景观图像:“s3image”,
SiteName:“Rojak Daily”,
SiteLogo:空
},
rojak_周刊:{//publisher 2
url:'xxx',
url_提要:“json”,
id:null,
姓名:'姓名',
描述:'Desc',
链接:“链接”,
DatePublishFrom:“pubDate”,
景观图像:“s3image”,
SiteName:“Rojak周刊”,
SiteLogo:空
}
}
}
const publishers=['rojak_daily'、'rojak_weekly']
函数getFeedType(发布者){
返回config.articles[publisher].url\u提要;
}
publisher.forEach(publisher=>console.log(getFeedType(publisher))代码>
可以通过变量访问对象的属性
您可以通过变量访问对象的属性谢谢。你是我的救命恩人。很好,谢谢你。你是我的救命恩人。它工作得很好
const config = require('@config'); // export from config file
class Main {
constructor(){
this.publishers = ['rojak_daily','rojak_weekly'];
}
// Main process
async startExport(){
try{
for(let publisher of this.publishers){
const feedType = await this.getFeedType(publisher)
const result = (feedType == 'rss')? await this.rss.start(publisher): await this.json.start(publisher)
return result
}
}catch(err){
console.log("Error occured: ", err)
}
}
// Get feed type from config
async getFeedType(publisher){
return await config.articles.rojak_daily.url_feed;
// this only return publisher 1 url feed.
// my concern is to dynamically passing variable
// into this config file (example: config.articles.<publisher>.url_feed)
}
}
module.exports = Main
async getFeedType(publisher){
return await config.articles[publisher].url_feed;
}