Extend configuration with custom settings loaded from file - node.js

I am trying to extend the configuration for my application with value loaded from a custom configuration file. We are using node-config and this is what I am trying to do:
// Server entry
const config = require('config')
// Merge setting from our custom configuration
const customConfig = require('../config/custom.json')
config.extendDeep(config, customConfig)
The above works, but since we are also using caching for the generated configuration file I will also have to do the above when ever the configuration is reloaded.
I know that I for instance could create a config/<hostname>.json which will get merged, but the custom configuration file is generated from another system and I am not able to change the name of the file in this setup.
Is there a better way of adding settings from a custom configuration file using node-config?

Related

Blazor server app cannot download .msg files

I have a Blazor Server 6.0 app where I have links to download .msg files.
I have setup IIS to serve that mime-type trying both application/octet-stream and application/vnd.ms-outlook (and restarting IIS)
I have also tried to put in web.config the staticcontent tag like suggested here:
.msg file gives download error
And obviously in my program.cs I have app.UseStaticFiles();
I try to put the .msg in a non-blazor app and they work ok, so I think is not IIS related
So why I cannot download (or open automatically in outlook) this type of file, while other (docx, pdf, zip, etc.) are Ok ?
ASP.NET Core -- on the server side -- also needs to know about the files it has to serve. You can enable serving all unknown file types (I'd rather not include the relevant code as it is a major security risk), or you can add you own additional mappings like so:
var provider = new FileExtensionContentTypeProvider();
provider.Mappings[".msg"] = "application/vnd.ms-outlook";
// app.UseStaticFiles();
app.UseStaticFiles(new StaticFileOptions()
{
ContentTypeProvider = provider
});
More info in the official docs: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/static-files?view=aspnetcore-7.0#fileextensioncontenttypeprovider
Additionally, Blazor Server registers custom options for serving static files (like .server.js, which is different from just .js). It's not directly exposed as a public API to configure, but you can look at the source here as to what the AddServerSideBlazor extension method actually does. The solution there relies on you calling UseStaticFiles without explicitly specifying the options, so that it can retrieve the StaticFilesOptions instance from DI.
Armed with this knowledge, you can override an already configured options instance as follows:
builder.Services.PostConfigure<StaticFileOptions>(o =>
{
((FileExtensionContentTypeProvider)o.ContentTypeProvider).Mappings[".msg"] = "application/vnd.ms-outlook";
});
This configures the already initialized options instance registered in the DI (after all other configurations happened on it, thus PostConfigure).
Note that if you would for whatever reason decide to use a different IContentTypeProvider, the unsafe cast above would need to be revised as well.

How to run second node.js server from same folder with alternative configuration

I would like to run a second node.js server (using same code) running from the same directory as the first. I use the default.json file in the config folder for the 1st server and use the config library, ie
let config = require('config');
for the first server.
What is the recommended way to specify an alternative config file. Could you specify a custom config file to use from the command line? How would I do this in node.js?
config is able to read different config files based on a few environmental variables.
Since you'll be running multiple instances on the same system, you'll need to change NODE_APP_INSTANCE:
Create config files like this:
config/default.json: Default configuration shared across all instances.
config/default-1.json: Specific configuration for instance 1. May override values of default.json and/or add new ones.
config/default-2.json: Specific configuration for instance 2. May override values of default.json and/or add new ones.
Start your node processes with individual values for NODE_APP_INSTANCE:
Instance 1: NODE_APP_INSTANCE=1 node index.js
Instance 2: NODE_APP_INSTANCE=2 node index.js
Checkout the docs for more info.

How to instantiate a blank workspace in node red?

I have an < iframe > based Node-Red UI hosted inside MEAN application. I want to create a blank workspace each time I visit the Node-Red hosted Page without restarting NR server?
But I also need to preserve the previously deployed flows.
I've tried changing user directory to scratch :
var settings = {
httpAdminRoot:"/red/",
httpNodeRoot: "/api/",
userDir:"/home/sudo/.nodered/scratch",
functionGlobalContext: { } // enables global context
};
but it wouldn't work instead it started creating persistent flow file inside the scratch directory.
You can't do what you want with the default Node-RED. It will always store the flow in a file in the userDir
You will need to look at implementing your own storage module as described here: http://nodered.org/docs/api/storage/

Keep configurations for DietJS server

Rather than having to change the URL passed in diet.listen() method on every server that I deploy my application on, there should be a better way to maintain such parameters in the application.
What options do we have to be able to manage such parameters?
You can create a '.json' file there at the root of the application and then do a require for the same. For example:
var configuration = require('./config.json');
The example expects you to save a file named 'config.json' with all your configuration as a JSON. The configuration object will hold all your settings that you might want to make dynamic and read at runtime.

Meteor 1.3 and configuration

i have a simple question.
When you use node + webpack you can easily configure whatever you want.
For example i can write in config default path for my app modules.
Haw can i do it in Meteor 1.3? do they have some config file such Webpack?
Meteor applications can store configuration options like API keys or global settings. An easy way to provide this configuration is with a settings.json file in the root of your Meteor application. The key/value pairs are available only on the server, but you can provide public access to settings by using public:
settings.json
{
"privateKey": "privateValue",
"public": {
"publicKey": "publicValue"
}
}
These values are available in your app using Meteor.settings.
From the Full Meteor Docs:
Meteor.settings contains deployment-specific configuration options. You can initialize settings by passing the --settings option (which takes the name of a file containing JSON data) to meteor run or meteor deploy. When running your server directly (e.g. from a bundle), you instead specify settings by putting the JSON directly into the METEOR_SETTINGS environment variable. If the settings object contains a key named public, then Meteor.settings.public will be available on the client as well as the server. All other properties of Meteor.settings are only defined on the server. You can rely on Meteor.settings and Meteor.settings.public being defined objects (not undefined) on both client and server even if there are no settings specified. Changes to Meteor.settings.public at runtime will be picked up by new client connections.
A good write-up can also be found on TheMeteorChef's Blog

Resources