Use NodeJS Helper Script in Meteor - node.js

I have written a small helper module in regular NodeJS to be used with NodeJS batch scripts. I've placed this and all the batch scripts in the "private" folder inside my Meteor project.
I'd like to also use the helper module on the server-side of Meteor as well, but I don't know the best way to handle that.
This is my current project structure:
client
... client files ...
private
scripts
helpers.js
batch_script1.js
server
... server files ...
So for Meteor to include the "helpers.js" file into the server, it either has to be located in the "server" folder, or imported via a package. Creating a symlink won't work, as multiple developers will be working on this and may have the repository checked out to a different directory location (seeing as how you need an absolute path to create a symlink).
I also don't want to have to duplicate the file and maintain two copies, so what are my options for sharing a helper script between a Meteor app and a NodeJS script?
Thanks

I was able to find help on the Meteor forums: https://forums.meteor.com/t/use-nodejs-helper-script-in-meteor/11056/3

Related

How to structure your NodeJS application in different modules?

so far i've learned a bit about NodeJS. But now i want to write a huge enterprise app with it and i'm wondering how to setup the structure correctly? Coming from other languages like PHP and Java, i imagine, i would split my project in different NPM modules. For example #mybigproject/customer, #mybigproject/cart and #mybigproject/checkout and so on.
But those submodules would be installed in the node_modules folder of the application skeleton. How would i tell for example Express, that the template files are in the different module directories? Or for example i use TypeORM for data access. So each module would have it's own set of models. How do those models know the database configuration data, as it's only in the main application skeleton, or the other way around, how does the application skeleton should know where to find the models?
Don't use npm modules for different parts of your project.
This components is integral part of your project and usually depend on your global config / schema / routing / etc
Just put it in different files and require it where you need it.
You can get an idea for folders structure from projects like Sail.JS
Use npm modules if you writing some utility that going to serve you for different apps and you want an easy way to upgrade the utility code once for all your apps (or in case you want to share that utility as open source for all of us)
NPM can install your local folder as a dependency. (ref)
npm install <folder>:
Install the package in the directory as a symlink in the current
project. Its dependencies will be installed before it's linked. If
sits inside the root of your project, its dependencies may be
hoisted to the toplevel node_modules as they would for other types of
dependencies.
Your module keeps its original location after installed and a symlink is created as the same name of your module folder in the top level node_modules folder.
In these custom sub-modules, you can use __dirname and relative paths to locate you configuration files to feed to database or other data consumers.
But remember that, sub-modules often serve as utility functions for the main module. They should be independent from the project context.

Config file in Node.js distributable package

I've been told to implement Continuous Integration for an existing application (FrontEnd: Node.js - BackEnd: .Net API).
The API endpoints are currently hardcoded in the .js files, that get "uglyfied" after the build (webpack). I want to move them to a config file, that gets copied to the dist folder, so they can be changed at deployment time (just like a Web.config file in the API).
I have zero experience with Node. Is this possible? How?
Look into a dotenv file and use process.env.MY_ENV_VARIABLE in your code to access environment variables. Here's one library https://github.com/motdotla/dotenv but I'm sure there others (can't remember which one I used in the past).
Edit: If you're using Webpack to bundle your front end app you might need to configure it to pull in environment variables, you can use the define plugin: https://webpack.js.org/plugins/define-plugin/

Running multiple node applications using same configuration file that is outside of each project

I am using pm2 to run several node applications. Problem is that I am using config files for every node application and so what I want to do is easily have say a json file outside of all the node application folders in which they can ALL point all for common database connections etc...
Prefer to not use linux environment variables unless there is an easy and great way of setting it up
pm2 does have the ecosystem, but it doesn't seem to be very well documented to me
what other solutions?
pm2 ecosystem // this generates .config.js not a .json
Create json or yml file. Put it in your root projects folder. And write "configProvider" which will read the file and populate configuration. It works really well for us. Especially this file can also been shared between different languages, not only javascript.

Ember-cli addon read configurations

I am trying to create an ember-cli addon and I want this addon to include custom command. This part I have already done by creating a lib/commands file structure and including the script that will run the command then used includedCommands in the addon index.js.
The part I am struggling with is I want the command to be able to read a configuration file within the host applications directory. How can I do this? If I could find out the host apps absolute path I suppose I could parse a file using built in node tools but how do I find this path?
If there are better ways to accomplish this task I am all ears.
In your commands run function, you can access this.project which provides details about the project.
What you want to use is this.project.root.. it's the root directory of the project.
I have managed to solve the problem. Don't know if it is the best way. So I am able to obtain the absolute process path in node just by using process.cwd() Then I can simply append a config file name to the end of the string to get my config file absolute path.
If there are any ember-cli specific ways that I should be doing this then please let me know.

NodeJS: How to create sym link for a directory

I am running kue node module in my app. Kue has its own UI. To use that UI we do this.
app.use(kue.app);
What that does it that it treats kue modules directory as my default directory. But I also want to use some custom routes to render and serve some pages of my own.
My custom templates reside in. /app-directory/views/
And the JS/CSS files reside in /app-directory/public/)
The issue is now that when I try to include any JS/CSS file in my jade template it is trying to look for that in public folder of the kue module.
/app-directory/node_modules/kue/lib/http/public/
I was suggested to create a symlink folder in kue's public folder that links it to main app's public folder. But I have never done that before, and can't seem to find any tutorial on google. So it would be great if someone can help out here or point to a link.
I don't know if it matters but the server is running on Ubuntu 14.04

Resources