Backend
You'll have to create a short route like this:
app.get('/foo/bar.json', function(req, res){
//...
res.send(data);
}
Frontend
$("#topics").select2({
placeholder: "Search for a movie",
minimumInputLength: 1,
ajax: { // instead of writing the function to execute the request we use Select2's convenient helper
url: "/foo/bar.json",
dataType: 'json',
results: function (data, page) { // parse the results into the format expected by Select2.
// since we are using custom formatting functions we do not need to alter remote JSON data
return {results: data};
}
},
});
And a JSON object can be/contain arrays. See https://ivaynberg.github.io/select2/#data