NodeJS: How to create sym link for a directory - node.js

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

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.

How to link standalone nodejs files to main nodejs server?

I am creating a node.js project with the ability to upload files and access a mysql database. The functions are finished and work as standalone files. So far I am able to access different html pages but I'm not sure how to include the nodejs upload and access functions. The functions will be used on different pages than the home page. All of the tutorials I've found establish a server, create the function and everything else all in one file. I'm assuming this is strictly for learning purposes.
After several hours of researching the structure of node.js, I realized the format of node.js projects is different than other languages. Modules are created, then imported into main to be called when necessary. I was expecting each page to have their own module imports declared in a separate page.

Front-end Node NPM Modules and multiple downloads of same dependency

Node/NPM newbie with a front-end dev question. I understand one of the strengths of an NPM-type module is that its dependencies get installed within itself, in node_modules. Modules always have the code that they need, and outside libs don't conflict.
That said, seems like this would result in the client downloading the same lib+ver (say, jquery v.X) multiple times. What's the technique for specifying that a module needs a dependency but that it shouldn't package that code if the dependency is already available on the site/page? Does said technique involve parent modules that make the shared lib+ver available?
Or, should various front-end modules just re-download the same lib+ver that other modules on the page might have already downloaded?
The client will only grab files from that folder that are needed, so if it's linked in HTML once the client will only grab it once. NPM handles dependency duplicates automatically.
Having said that, normally you will want to only serve a static folder to the client without revealing your entire server structure. This can be achieved using:
app.use(express.static('server/public')
where 'server/public' is the directory relative to the server.js file that you want to serve. In this case, 'public' contains all my linked view files, stylesheets, JS files, etc. that are linked from the HTML pages. You don't need to move that module's dependencies there as well.
The downside to this is that you'd have to manually move dependencies into the public folder (I make a 'vendor' directory usually) and link from there. It's more work but it's much more efficient and safer in the long run.
NOTE: when using a static folder to serve files, your HTML links will be served from a relative path to that folder.

Static folder in NodeJs

I suppose my question is basic but I'm new to Node.js infrastructure and feel very confused.
I have a simple web-site where all my js files are bundled by webpack into bundle.js that is available by /static/bundle.js path (according to webpack configuration).
But now I have another folder with static data in the root of my project and I want to make it publicly available by /static2/... address. How can I do this? Who is responsible for that (webpack, nodejs, etc.)?
If you use express - you can take a look here:
http://expressjs.com/en/starter/static-files.html
For example:
app.use(express.static('public'));

Use NodeJS Helper Script in Meteor

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

Resources