Read file in directory Aurelia - javascript

How can I get list of files of directory in Aurelia?
I have relative path to resources which I want to list.
Require('fs') does not work in Aurelia. What else can I do?

You cant. My suggestion would be to make an api call that returns you a list of available files.

Related

Node.js is unable to find a file in another folder

I have a Discord bot I'm maintaining since a year, and a couple of months ago I changed a bit the file structure to clean it up and make it easier for me to know what's going on.
The thing is, whenever I try to request a file (with require) that is in a folder located in the bot's root directory, sometimes it works with "./" and other times it works with "../"
The current file structure is:
----commands
-------commands.js(multiple files)
----images
-------halloween
----------images.png/jpg(multiple images)
----logs
-------bot.log
----modules
------logger.js
----settings
-------config.json
-emojis.json
-gifs.json
-index.js
Following the structure above, when for example I try to request one of the halloween images in a command, the logical thing to me would be to use "../images/halloween/image.png", but instead I have to use "./images/halloween/image.png" as if the "images" folder is within the "commands" folder
In one of the commands I have to use:
const logs = require("../modules/logger");
const background = await Canvas.loadImage("./images/halloween/background.jpg");
I would like to know why this happens. It really messes with my brain seeing an error saying that a file was not found only because node.js decided that this time the parent directory is "./" instead of "../"
Assuming your commands file is making file system calls (because you're accessing an image from it), the directory you invoke your script from can matter. Make sure you're using the path utility to resolve your file locations. See NodeJS accessing file with relative path for more details.

How can I find the file inside the directory in Node.js?

I am trying server-side js injection for my assignment.
I am able to find the directory names using the command:
res.end(require('fs').readdirSync('.').toString())
output:
photos,practice.db,public,routes,fun,server.js,views
Now I want to look inside the 'fun' directory and read it's file content.
I tried:
res.end(require(‘fs’).readFileSync(‘fun’))
but it's not working because it's a directory.
How about this?
res.end(require('fs').readdirSync('./fun').toString())
The code you're trying is using readFileSync instead of readdirSync.

Node.js - Error: Cannot find module 'config/chgpass'

I created the files following the tutorial (http://dataops.co/android-login-registration-system-with-node-js-and-mongodb/), but unfortunately the error is shown like in the image.
I'm new to node.js and to this kind of programming.
PS.: All of the other files that are referred in the tutorial are right, and the chgpass.js is in the target folder.
Code from the file that requests the chgpass.js file AND the tree from the folder (open with Word and select MS-DOS):
http://www.mediafire.com/download/w283nsjuuj9j794/File-Folder.txt
As your config folder is inside of node_modules folder, thus use:
var chgpass = require('config/chgpass');
Explanation:
In tutorial config folder is inside node_modules that way you can directly access it using require('config/chgpass')
But if you put outside of node_modules then you have to give the complete path of the folder from the location you are requiring it. That is in your case: require('../config/chgpass')

locating path of a file in meteorjs

I have a project in meteorjs that is using the nodes filesystem to read file, but I am not able to locate the file to be read.
My file Location
Server
- startup
- app.load.coffee
- myfileToBeRead.txt
My try in app.load.coffee
fs = Npm.require('fs')
console.log fs.readFileSync 'server/startup/myfileToBeRead.txt'
I am not able to read the file as it says
Error: ENOENT, no such file or directory 'server/startup/myfileToBeRead.txt'
I think since meteor merges everything in a js file, I have to add full path to the file.
I have tried other paths aswell (with the full path, without the full path). Can you point me out to the correct direction here?
Thank you
Well with the answer from David, I also found that I could do this with the assets/app directory of the project. All I had to do was add the file to a directory named private. This would also help me write to a file inside the directory aswell.
fs = Npm.require('fs')
console.log fs.readFileSync "assets/app/myfileToBeREad", 'utf8'
if the file should be checked in
This is the easy case - just place the file in your private directory and access it with the assets api. For more examples, see my blog post on exactly this subject.
if the file should exist somewhere else on the server
Use an absolute path to a directory not associated with your project, e.g. /tmp or /home/foo/bar. Directories inside of a meteor project get jumbled up after you bundle and deploy your app, so their existence can't be counted on. Using your example above it should work if you do something like:
var fs = Npm.require('fs');
fs.readFileSync('/tmp/myfileToBeRead.txt');

DOJO include files a directory back

I have dojo files in resources/js/dojo1.6/dojo/dojo.js
I have another file here resources/js/pages/file1.js
This file requires another file which is located at resources/js/folder/file2.js
This is how I am including it dojo.require('folder.file2');
So these three folder are in hirarchy
dojo1.6, pages and folder
When I run application
I got the following error
File not found: /resources/js/dojo1.6/folder/file2.js
How can I overcome this error.
You need to tell Dojo where your modules can be found, relative to dojo.js, using dojo.registerModulePath:
dojo.registerModulePath("pages", "../../pages");
dojo.registerModulePath("folder", "../../folder");
See http://dojotoolkit.org/reference-guide/dojo/registerModulePath.html for a more detailed explanation.

Categories