include a js file in routes file node (not node mudule) - javascript

I am trying to create a file that will contain some functions that will be used throughout the entire project. I would like these functions to have access to all the modules that I am using in the project(which is why I don't want to use another module)
in my app.js I can use require to a static folder containing the js file and it works fine however if I go to routes/index.js and try to require the same folder it cannot find it.
Im guessing thats because its not leaving the routes folder. How can I move to the parent of the routes folder.
I have tried
require('controllers/cf');
require('./controllers/cf');
require('cf')
require('../controllers/cf');
require('../~path to project~/controllers/cf');
none of these work. I know this is simple however the solutions I have found dont work unless I make a node module then it has no problems but then other modules are not within my scope

This will work.
var path = require('path');
require(path.join(__dirname, '../controllers/cf'));

Related

Converting html with css and js code pages into a node js application

I have multiple HTML pages with CSS and js files that work(if I open an HTML page on its own using chrome for example) I have tried to use these pages inside a node js project I created.
but for some reason, it doesn't recognize the js file and doesn't apply the CSS design.
I had installed path, but I still can't get node js to recognize the js file.
I'm referring to the js file inside the HTML code by
You need to let the node know that you want to use these files in your project. Or you have to get permission to use these files from the node.
To do this, create a directory called 'public' in the root path of your project
And use the following code in the main file of your project
const path = require('path');
const express = require('express');
app.use(express.static(path.join(__dirname, 'public')))
The above code means that any file in the public folder (such as js css img) is allowed to access and use

ember.js include custom js

Attempting to wrap my head around Ember.js.
Seems I understand the complex things, but miss out on the little things.
How would one go about adding an example.js file?
For simplicity, let's say the example.js file only contains:
(function(){
console.log("example is alive in console");
})(window);
This should display "example is alive in console" within the browser console.
I have tried:
adding app.import('vendor/javascripts/example.js'); within ember-cli-build.js and adding <script src="{{rootURL}}vendor/javascripts/example.js"></script> to index.html
Console is showing
ⓧ GET http://localhost:4200/vendor/javascripts/example.js
DEBUG: -------------------------------
DEBUG: Ember : 2.11.3
DEBUG: Ember Data : 2.12.1
DEBUG: jQuery : 3.2.1
DEBUG: -------------------------------
ⓧ GET http://localhost:4200/vendor/javascripts/example.js
All of the answers I have found stated that just adding custom.js to vendor file works. Sadly, I am missing something.
When modifying ember-cli-build.js you MUST RESTART the ember server manually. The livereload server will not pick up the changes.
This works for me when I don't nest assets in the /vendor directory. The ember-cli build process bundles JS files in /vendor into a single vendor.js file, which you can see linked in app/index.html. So place your example.js file at the root of /vendor, and then add the import to ember-cli-build.js:
app.import('vendor/example.js`);
Now when you start the server, your code from example.js should execute, since it will be included in assets/vendor.js.
Firstly, Ember.js has Convention Over Configuration approach, and your URL can do a lot of things than a normal HTML website.
Whatever you may want to do with your custom.js file it is not ember way of having it as a path. You need routes for navigation across the app. Although routes do much more than navigation. You specify the structure of your app that a user can browse through using Router's map function in app/router.js file.
However if you want to include custome.js file in your app, and have custom.js do some set of tasks for your app. You can simply go ahead and create a directory with any name, javascript for instance inside app directory. Have your javascript files placed inside it. Then you can import these files as simply as referencing any other files in ember:
import customObject from 'yourApp/javascript/custom.js';
Here, your custom.js should be exporting customObject.
Do read the guides if you want to learn more. And the API docs if you actually want to learn more.
Note: At the time of writing this answer current ember-cli version is #2.12.0

How to use multiples files for the Meteor server?

I'm trying to split my server in multiples files, and actually I want to make cmdb_login.js and main.js.
The thing is that I don't know and I don't find how to say in main.js, use cmdb_login.js.
I have tried import './cmdb_login.js';
My app structure is:
If you need the code of the two .js files just ask me.
Thank you for the help
As always, Meteor combines all files eagerly, unless they are placed in a path which includes a folder named imports.
I.e. in your case, your cmdb_login.js will be combined (and then executed) before main.js in the server JavaScript code.
A proper way is to put almost all your files in an imports folder, and explicitly call them in a single main.js file (or whatever its name, provided that it is not in an imports folder), using an import statement like the one you tried (e.g. import './imports/cmdb_login.js').

What is the difference between JS files in dist/ folder and the one in root?

I am totally new to NodeJS and I wonder what's the difference between those two.
For example, in this project (https://github.com/fikriauliya/hipku), we have index.js and dist/hipku.js. They are similar except the last line:
module.exports = publicMethods; vs return publicMethods;
I guess dist/hipku.js is generated from index.js? How is it generated and why does it need to be generated?
Things in the dist folder are usually the product of building from index.js in this case. You'll notice it gets minified, and that folder would eventually be used on production sites. If you look at the package.json file, you'll notice that index.js is the main file, so if you're doing any edits, that would be the place to do so.
It depends on how you want to use this package, in browser or server side.
server side
index.js is the entry of NPM package. When you do require('hipku'), actually NodeJS locates the file module node_modules/hipku and run index.js ends up with the object adhere to module.exports
browser
Just load dist/hipku.js into your browser by <script>, it will register hipku into your global namespace, then you can use it's API.

How do I make text files accessible to the server on Meteor

I'm surprised I can't google my answer here... it seems no one else is having the issue.
When you run the meteor service the js, html, etc. is packaged in the .meteor/local/build folder, but it appears to exclude stuff that isn't js or html. I have a folder called "magicsets" and one called "magicimgs" and neither are in the /local/build folder. This is obviously why, when i attempt to use fs to readfile, it fails to find the file "magicsets/M14.json"
I tried putting the magicsets folder into a folder named "private", but that didn't accomplish anything.
How do I make files accessible locally to my server via FS and how do I make files accessible publically to my server via raw urls?
I'm sure I'm missing something very simple, because there are lots of more complicated questions and answers on SO, yet there is no answer for this. Thanks.
Meteor 0.6.5 which was released yesterday has a new feature which helps loads with this.
Make a directory called /private which you can access with the new Assets.getText or Assets.getBinary functions.
The stuff in the /private directory will then be bundled up into a directory called assets in /program/server/assets and it will not be accessible to the web & you wouldn't need to worry about using fs either. You could just use Assets.getText instead
To make a publicly accessible file put it in /public. So if you had a.jpg at /public/a.jpg it would be accessible at http://yourdomain.com/a.jpg
If you want text files to be available to the webserver i.e. the server that defaults to port 3000, create a folder called public in the root of the project/app directory. drop your folder and files there. You would then be able to access them as http://localhost:3000/magicsets/M14.json
update: it looks like can override the bundler, but it does require changing some of the core code there's no .meteorignore file yet. check this SO answer out: https://stackoverflow.com/a/16742853/105282
To serve a directory of files publicly independent of what Meteor is doing, you can use the following approach. I do this, for example, when I need to link an entire (Javascript) git repo into my Meteor app so I can work on a checked out version of the library.
The following works for 0.6.5. It basically servers up a checked out folder of OpenLayers in /lib:
connect = Npm.require('connect')
RoutePolicy.declare('/lib', 'network')
WebApp.connectHandlers
.use(connect.bodyParser())
.use('/lib', connect.static("/home/mao/projects/openlayers/lib"))
For more information, see https://github.com/meteor/meteor/issues/1229.

Categories

Resources