Storing Node.JS files Within a Meteor.JS Project - javascript

I have a meteor.js project directory (created from meteor create projectName) and I have some regular node.js .js files related to this site that only needs to be run using node fileName.js or using forver.
Is there a way to store these .js files within the Meteor project directory without it being auto-run (like how it will be executed if placed in the /server directory)?
And is it more advisable to place these .js files outside of the Meteor project directory?

Taking a look at the meteor documentation, you could probably use the private folder. This should stop auto-execution as well.
http://docs.meteor.com/#/full/structuringyourapp
private: All files inside a top-level directory called private are only accessible from server code and can be loaded via the Assets API. This can be used for private data files and any files that are in your project directory that you don't want to be accessible from the outside.

Related

Including source files in Cordova project from outside the project's directory hierarchy

tl;dr: I want to include some JavaScript files from another project in another directory into a Cordova project, without (manually) copying those files into my Cordova project directory tree. How?
I am editing an existing Cordova project that makes use of some JavaScript source files originally written for a large web application project.
Currently, I am adding these source files by copying them to a subdirectory of the Cordova project, and by adding a <script> tag with the relative path to an index.html file in the Cordova project. This causes the JavaScript files to integrated in the deployed app, apparently by virtue of Cordova's (or Ionic's?) magic.
Now, it would be preferrable for me to avoid copying the JavaScript files to my Cordova project directory. Instead, I would like to directly link to the files in their original location rather than copying them (so updates from the web application get automatically applied to the Cordova project, as well).
As both the Cordova project and the large web application reside in the same VCS repository, the relative path to the files will always be the same - for the sake of an example, like this:
|-repository/
|-web/
| |-src/
| |-myCode.js
|-cordova
|-src
|-index.html
|-helpers
|-myCode.js // copy of /repository/web/src/myCode.js
Therefore, I have tried setting the relative path in the aforementioned index.html file - but unfortunately, Cordova seems to take over that file verbatim, and thus, the resulting URL will not exist (prompting a 404 error in the console, and causing the code from the file to be unavailable).
How can I have Cordova include and reference files from outside the directory hierarchy of a Cordova project?
Is there maybe at least a way to have Ionic or Cordova automatically copy the files into the Cordova project directory upon building the app?

Where to put compiled JS in NodeJS app

I am building a NodeJS application and I am not sure where I should compile my Javascript to (i.e. minified and uglified Javascript) and how to structure my app.
All the examples I have found online say to simply make a /public directory and put all the JS in there.
The problem with that is that even if I serve the Javascript file app.min.js from /public/js, it is still really easy for someone to figure out that at public/js/app.js he can find all my original javascript code and steal it.
What is the proper way to structure the app so I can separate development files and distribution files?
You put your public JS files in a directory that your node.js server serves up to the public and you put your private source files somewhere else that your node.js server does NOT give any access to the public.
Simply don't put private files that you don't intend to share anywhere in the same hierarchy as your public files that your node.js has routes configured to serve.
Remember, your node.js server ONLY serves files or directories that you have explicit routes for. So, just make sure the private files are somewhere other than those directories containing publicly served files.
There are lots of different ways to structure it (and none is any more "right" than any other as it depends upon what else you have in your file structure). For a simple system, I create a specific "public" directory below my server and I put all public stuff off of that. I can then use express.static() routes for the public hierarchy and nothing private ever gets shared.

jspm config to load files outside baseURL

I'm developing a JS app http://myclibu.com which shares code between Node.js on the server and code in the Browser. And I'm migrating from using require.js to jspm.io for module loading in the Browser.
My ideal folder structure is
app -|- browser
|- server
|- shared
However so far I can't get config.paths in JSPM to work with files outside of the browser folder (outside of baseURL) which means I need to copy shared into browser to get files to load.
Is there any way I can use the folder structure as shown above? Note that I'm developing on Windows, in case that matters.

How do I obtain the path of a file in a Meteor package?

I know how to get the current directory from a Meteor package, but how do I get the path of a specific file in the project?
node's __dirname and __filename don't work in Meteor.
It is complicated.
meteor run copies your project files to a tree of directories inside <project-dir>/.meteor/local/build, reorganizes them in non-obvious ways (e.g.. the private subdirectory in the original tree becomes the assets subdirectory) and mixes it in with various npm modules to create a bundle that can be executed as a nodejs project. Indeed, to avoid duplications, there is a .gitignore file automatically set up in the .meteor directory that tells git, if you use it for version control, not to copy the .meteor/local directory.
The original project directory gets watched in case you change a file. The change then gets copied into the current project build directory and the project rebuilt.
If you deploy to a remote system, the build gets copied to a server and then run.
process is usually a defined global server-side object, and works according to the node.js API, because the meteor server code is ultimately running in node.js.
So you can run console.log(process.cwd()); in your server-side to obtain the current working directory for the server process, usually something like:
~/<meteor project directory>/.meteor/local/build/programs/server
This suggests that when meteor run is done locally, original project files are in ../../../../../, but don't use that as it may change in the future.
Instead, for the directory containing the original project files, you could use:
baseDir = process.cwd().replace(/\/\.meteor.*$/, '');
This will get the working directory, and truncate everything beginning with /.meteor
This won't work for a server deploy, though, because the original project tree is not needed on the server, only the build. Files that aren't intended to be client or server code could possibly be stuck in the private subdir, which as I mentioned becomes the assets subdir in the build. Ways to currently find files in the build is either manual inspection .meteor/local in a local run, or use of a JS library that calls or imitates gnu find.
Since you mentioned packages, I note that in the build, server-side package code finally ends up in:
~/<project-dir>/.meteor/local/build/programs/server/packages
and client side in:
~/<project-dir>/.meteor/local/build/programs/web.browser/packages

MeteorJS Change Web Root

Currently my app structure looks like this-
.meteor/
client/
... public files
server/
typings/
tsconfig.json
And when I run meteor with the meteor command it serves my project from this directory. So client files are accessed via client/assets/images/myimg.png. Not only is the client portion redundant but It seems like a security issue as I'm then assuming server/* is also accessible. How can I update meteor so the web root begins at client/ ?
I did try google first. I either couldn't find anything or didn't understand what I was reading.
Thank you!
The convention for storing public assets (e.g. an image) is to put them in the root level folder public. If you put image.png in the public folder, it will be accessible at /image.png. You'll also want to look into using a router to appropriately serve your routes/assets.

Categories