I'm using nestjs 8. I want to access and read file from assets directory.
const fs = require('fs');
const data = fs.readFile('assets/audio/sound-1.mp3')
With this code it doesn't find the file.
How can I access resource file from assets directory?
Thanks in advance
Related
I'm reading json file in my file directory and works file in
const data = await readFile(join(process.cwd(),'./src/test/data/test/test.json'), 'utf8')
It is not being read in production in vercel.
Tried by directly giving the file path without join.
Well, this would mean that the file path you're trying to read doesn't exist on your production server. Find the correct path and re-adjust the path string
var fs = require('fs');
var obj = JSON.parse(fs.readFileSync('./config.json', 'utf8'));
This is the code for my app (index.js) . It uses a file named config.json using a relative path ./config.json. When I compile this into my mac using $pkg index.js, I obtain a test-macos file that is my app. Both the app and the config.json file are located in the same folder named project (this is why I can use the relative path ./config.json). However, once runnning the app I run into an error:
/Users/amelie/Documents/project/test-macos ;
exit; internal/fs/utils.js:312
throw err;
^ Error: ENOENT: no such file or directory, open './config.json'.
Therefore it seems that my app cannot find the config.json file. I could determine an absolute path but I need these two files to always be together because I need the path in the variable
var obj = JSON.parse(fs.readFileSync('./config.json', 'utf8'));
to be always the same since I share those files to other people and the absolute path would therefore change. Is there a way to do this ?
I want to implement universal links in my project and I need to serve a json via Koa, which is a static file named apple-app-site-association.
My file is located in /assets/apple-app-site-association/apple-app-site-association folder.
My concern is that I cannot access this https://myprojectlink/apple-app-site-association.
What I have at this moment:
const path = require("path");
const Koa = require("koa");
const mount = require("koa-mount");
const serve = require("koa-better-serve");
app.use(mount("/apple-app-site-association", serve(path.resolve(__dirname,"../../../assets/apple-app-site-association/apple-app-site-association"))));
I get Not Found, it seems like I cannot serve it in the right way.
What can I do?
Thank you very much in advance.
The koa-static module is what you are looking for. You can use this to serve a single file or entire directory to a given path. Here are a couple of examples that should help:
Serving Files With koa-static
To serve files just pass the koa-static middleware to the koa's middleware stack with app.use().
Serve an Entire Directory
Here we are serving the entire /static directory
const Koa = require('koa')
const serve = require('koa-static')
const path = require('path')
const app = new Koa()
app.use(serve(path.join(__dirname, '/static')))
app.listen(3000)
Serve a Single File
Here we are serving a single file, for example, the data.json file inside of the /static directory
const Koa = require('koa')
const serve = require('koa-static')
const path = require('path')
const app = new Koa()
app.use(serve(path.join(__dirname, '/static/data.json')))
app.listen(3000)
Serve Directory or File on a Given Path
Use koa-mount to mount koa-static to a given path. For example, here we mount the entire /static directory to be served on the /public path
const Koa = require('koa')
const serve = require('koa-static')
const mount = require('koa-mount')
const path = require('path')
const app = new Koa()
app.use(mount('/public ',serve(path.join(__dirname, '/static'))))
app.listen(3000)
serve (koa-better-serve), like most static server middlewares for Node frameworks, takes a path to a directory, not a path to a single file. You can also get rid of the mount() call, koa-mount is for mounting other Koa apps as middleware.
app.use(serve(path.resolve(__dirname,"../../../assets/apple-app-site-association/")));
The official method for static-file serving is https://www.npmjs.com/package/koa-static, you can see the documentation there.
I have an http cloud function that returns some dynamic HTML. I want to use Handlebars as the templating engine. The template is sufficiently big that it's not practical to have it in a const variable on top of my function.
I've tried something like:
const template = fs.readFileSync('./template.hbs', 'utf-8');
But when deploying the function I always get an error that the file does not exist:
Error: ENOENT: no such file or directory, open './template.hbs'
The template.hbs is in the same directory as my index.js file so I imagine the problem is that the Firebase CLI is not bundling this file along the rest of files.
According to the docs of Google Cloud Functions it is possible to bundle local modules with "mymodule": "file:mymodule". So I've tried creating a templates folder at the root of the project and added "templates": "file:./templates" to the package.json.
My file structure being something like this:
/my-function
index.js
/templates
something.hbs
index.js //this is the entry point
And then:
const template = fs.readFileSync('../node_modules/templates/something.hbs', 'utf-8');
But I'm getting the same not found error.
What is the proper way of including and requiring a non JS dependencies in a Firebase Cloud Function?
The Firebase CLI will package up all the files in your functions folder, except for node_modules, and send the entire archive to Cloud Functions. It will reconstitue node_modules by running npm install while building the docker image that runs your function.
If your something.hbs is in /templates under your functions folder, you should be able to refer to it as ./templates/something.hbs from the top-level index.js. If your JS is in another folder, you might have to work you way out first with ../templates/something.hbs. The files should all be there - just figure out the path. I wouldn't try to do anything fancy is your package.json. Just take advantage of the fact that the CLI deploys everything but node_modules.
This code works fine for me if I have a file called 'foo' at the root of my functions folder:
import * as fs from 'fs'
export const test = functions.https.onRequest((req, res) => {
const foo = fs.readFileSync('./foo', 'utf-8')
console.log(foo)
res.send(foo)
})
The solution was to use path.join(__dirname,'template.hbs').
const fs = require('fs');
const path = require('path');
const template = fs.readFileSync(path.join(__dirname,'template.hbs'), 'utf-8');
As #doug-stevenson pointed out all files are included in the final bundle but for some reason using the relative path did not work. Forcing an absolute path with __dirname did the trick.
I have an electron app that uses an app.asar file. I want to be able to open files (.xlsx) that are in the asar file in the default Windows application (which is Excel). I have tried
let xlsx = path.join(__dirname + '/path/to/app.asar/path/to/file');
shell.openItem(xlsx);
but it doesn't work (no error, the file doesn't open).
I can read the file with fs
let xlsx = path.join(__dirname + '/path/to/app.asar/path/to/file');
fs.readFileSync(xlsx);
but I can't get the file to open in Excel.
From the documentation for Application Packaging
An asar archive is a simple tar-like format that concatenates files into a single file. Electron can read arbitrary files from it without unpacking the whole file.
Only Electron can access those files, Excel and other applications simply can not deal with asar archives.
You could copy the file from the asar archive to the system's temp folder and open it from there. Like this:
const fs = require('fs')
const path = require('path')
const {shell, app} = require('electron')
let xlsx = path.join(__dirname,'/path/to/app.asar/path/to/file')
let xlsxtmp = üath.join(app.getPath('temp', 'file')
let ws = fs.createWriteStream(xlsxtmp)
fs.createReadStream(xlsx).pipe(ws)
ws.on('finish', () => {
shell.openItem(xlsxtmp);
}
Another option would be to not pack that xlsx into the archive and instead download it into the userData path.