A laravel command to expose the configuration keys you define to the commandline. Below a NodeJs function that calls the command and parses the response
// Create a new command in your laravel installation
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class NodeJs extends Command
{
/**
* Here you define all the config keys you want to share with NodeJs
*/
protected $keys = [
'pusher' => 'broadcasting.connections.pusher',
'mysql' => 'database.connections.mysql'
];
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'NodeJs:config';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Get laravel settings in NodeJs';
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$config = [];
foreach($this->keys as $key => $value){
$config[$key] = config($value);
}
$this->info(json_encode($config));
}
}
?>
// In your NodeJs app add the following function:
var laravelSettings = function(callback){
require('child_process').exec('php artisan NodeJs:config', function(error, stdout, stderr) {
if (error !== null) {
console.log('exec error: ' + error);
return false
}
callback(JSON.parse(stdout));
});
};
// And use it
laravelSettings(function(settings){
// Settings will be in object format
console.log(settings);
});