简体   繁体   English

动态返回配置值(node.js)

[英]Dynamically return config value (node.js)

I'm building a content middleware which gather contents from our external publishers.我正在构建一个内容中间件,它从我们的外部发布者那里收集内容。 The publishers will share their contents either in rss or json and the key/value field would be different from each other.发布者将在 rss 或 json 中共享他们的内容,并且键/值字段将彼此不同。 To make thing easier, I created a config file where I can pre-defined the key/value and the feed type.为了让事情变得更简单,我创建了一个配置文件,我可以在其中预定义键/值和提要类型。 The problem is, how can I dynamically return this config value based on publishers name.问题是,如何根据发布者名称动态返回此配置值。

Example: To get Publisher #1 feed type, I just can use config.articles.示例:要获得 Publisher #1 提要类型,我可以使用config.articles。 rojak_daily .url_feed rojak_daily .url_feed

my config file /config/index.js我的配置文件/config/index.js

module.exports = {
    batch:100,
    mysql: {
        database: process.env.database,
        host: process.env.host,
        username: process.env.username,
        password: process.env.password
    },
    articles:{
        rojak_daily:{ // Publisher 1
            url: 'xxx',
            url_feed: 'rss',
            id: null,
            Name: 'title',
            Description: 'description',
            Link: 'link',
            DatePublishFrom: 'pubDate',
            LandscapeImage: 's3image',
            SiteName: 'Rojak Daily',
            SiteLogo: null
        },
        rojak_weekly:{ // publisher 2
            url: 'xxx',
            url_feed: 'json',
            id: null,
            Name: 'Name',
            Description: 'Desc',
            Link: 'link',
            DatePublishFrom: 'pubDate',
            LandscapeImage: 's3image',
            SiteName: 'Rojak Weekly',
            SiteLogo: null
        }
    }
}

my main application script我的主要应用程序脚本

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; 
}

You can access properties of objects by variable您可以通过变量访问对象的属性

You could either loop over the articles by using Object.entries(articles) or Object.values(articles) in conjunction with Array.prototype.forEach() , or since you already have the name of the publisher you could access that entry with config.articles[publisher].url_feed , like so:您可以使用Object.entries(articles)Object.values(articles)Array.prototype.forEach()一起循环文章,或者由于您已经有了发布者的名称,您可以使用config.articles[publisher].url_feed访问该条目config.articles[publisher].url_feed ,像这样:

 const config = { articles: { rojak_daily: { // Publisher 1 url: 'xxx', url_feed: 'rss', id: null, Name: 'title', Description: 'description', Link: 'link', DatePublishFrom: 'pubDate', LandscapeImage: 's3image', SiteName: 'Rojak Daily', SiteLogo: null }, rojak_weekly: { // publisher 2 url: 'xxx', url_feed: 'json', id: null, Name: 'Name', Description: 'Desc', Link: 'link', DatePublishFrom: 'pubDate', LandscapeImage: 's3image', SiteName: 'Rojak Weekly', SiteLogo: null } } } const publishers = ['rojak_daily', 'rojak_weekly'] function getFeedType(publisher) { return config.articles[publisher].url_feed; } publishers.forEach(publisher => console.log(getFeedType(publisher)));

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

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