Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
186 views
in Technique[技术] by (71.8m points)

node.js - How to make and reference a configuration file in javascript

I have a file that I am trying to make a config.json file for to be able to input and change the data quick and easily. The code below is my mention.js file that I mapped and referenced into my index.js File. The data below needs to be changed periodically and and a config file that I can set the 1. accountName and 2. userId Into my mention.js file. I’m new to npm and JavaScript and am still learning so any help and advice would be extremely helpful. Thank you!!

const map = new Map();
map.set("Pogo0303PTA",'<@679533427925450852>');
map.set("Wretched0671pta", "<@679533427925450852>");
map.set("Pogo0347PTA", "<@679533427925450852>");
map.set("Wretched0047pta", "<@679533427925450852>");
map.set("PogoDroid","<@679533427925450852>");
module.exports = map;```
    
     ```{
"acct1": "pogopta000",
"userid1": "65465132168451324"
},
{
"acct2": {account}
"userid2": {userID}
} ETC...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you only need to read the config.json file once, you can use const config = require('./config.json'), assuming you have the .js file in the same folder as config.json.

If you need to read config.json you can use node's built in module fs by putting const fs = require('fs') at the top of your program and using let config = JSON.parse(fs.readFileSync('./config.json')) to read the file, and using fs.writeFileSync('./config.json', JSON.stringify(config)) to write the file.

You can structure your json file like:

   [
       {
            "accountName": "Pogo0303PTA",
            "userId": "<@679533427925450852>"
        },
        {
            "accountName": "Wretched0671pta",
            "userId": "<@679533427925450852>"
        },
        etc...
    ]

Hope this helped!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...