Reputation: 17
If I have a number of responses:
const exampleResponses = [ "name1", "name2", "name3"];
But let's say it's thousands of responses, not just three. Is there a way to pull in the responses from another file? As it stands, with thousands of responses it just clogs up my coding file.
Upvotes: 1
Views: 152
Reputation: 6710
There are many ways you can go about this. One way would be to have a separate file with one array containing all the responses, then export that variable and require it in your main file
Responses.js
const responses = ['r1', 'r2', 'r3']
// And so on...
module.exports = {
responses
}
Main File
const { responses } = require('filePathToResponsesJS')
Upvotes: 2
Reputation: 83
A way to do this is to make a JSON file and call from it with JavaScript.
const { exampleResponses } = require('./example.json')
{
"exampleResponses": [
"name1",
"name2",
"name3"
]
}
Upvotes: 2
Reputation: 110
You can import the FileSystem module and do something like this:
responses= JSON.parse( fs.readFileSync( 'data/responses.json', function( err, data ) { ... }
Similarly, you can write to said file to keep things neat and tidy.
Upvotes: 1