I hope I can sum up my question with as much detail as possible. I am new to NodeJS so be nice :D
I am writing an app in node that will allow people to see stats on their Plex Media Libraries. I am storing my configuration in a file config.ini. Settings in the file include: the url base of the application, the port the app will run on, their Plex username & password, number of movies or episodes to display on the index and a few other settings. I have a config template & route that allows them to update these settings and the file get updated with the new values.
My problem lies with how do I get this file to be read dynamically by Node. When the person updates the config thru my config.js router it takes restarting the app to get the new values read. (Somethings like url_base/port need to have the app restarted, I'll take care of this later) but simple things like changing the amount of movies or episodes to display shouldn't need a restart.
For example take a look at my index.js route which displays a small amount of information:
var express = require('express');
var Q = require('q');
var util = require('util');
// Define router
var router = express.Router();
// Load config
var UserConfiguration = require('../lib/config');
var config = new UserConfiguration();
// Load plex
var Plex = require('../lib/plex');
var plexClient = new Plex({
hostname: config.getPlexHost(),
port: config.getPlexPort(),
username: config.getPlexUser(),
password: config.getPlexPassword(),
token: config.getPlexAuthToken()
});
// Plex is down or we couldn't get in
if(plexClient.ping() !== true) {
res.render('pages/index', {
page_title: 'home',
plexRecentMovies: null,
plexRecentEpisodes: null
});
}
// Retrieve new episodes, pass in config setting for limit
function recentlyAddedEpisodes(req, res, next) {
var recentEpisodes = plexClient.shows()
.then(function (showSection) {
return showSection.key;
})
.then(function(key) {
plexClient.getRecentlyAddedEpisodes(key, config.getRecentEpisodes())
.then(function(episodes) {
req.recentlyAddedEpisodes = episodes;
return next();
});
});
}
// Retrieve new movies, pass in config setting for limit
function recentlyAddedMovies(req, res, next) {
var recentMovies = plexClient.movies()
.then(function (movieSection) {
return movieSection.key;
})
.then(function(key) {
plexClient.getRecentlyAddedMovies(key, config.getRecentMovies())
.then(function(movies) {
req.recentlyAddedMovies = movies;
return next();
});
});
}
function renderIndexPage(req, res) {
res.render('pages/index', {
page_title: 'home',
plexRecentMovies: req.recentlyAddedMovies,
plexRecentEpisodes: req.recentlyAddedEpisodes
});
}
// Finally render the template
router.get('/',
recentlyAddedEpisodes,
recentlyAddedMovies,
renderIndexPage
);
module.exports = router;
I am positive I am not doing this the correct way because everything outside of the router.get() seems to be cached, so the new values never get applied even thou I make calls to config.getSomeSetting() in my other two functions.
Thanks for any help, let me know if I should provide any more information.
You could switch to json instead of ini and then use my json-update module.