Getting files from file share using Express res.download - javascript

I have combed the internet looking for a solution to this, but everything I see points back to serving static files which doesn't appear to do what I want. I'm trying to have my Express server reach out to a local file share using the following:
res.download(pathToFile, fileName, (err) => {
if (err) throw err;
});
pathToFile is the absolute path to the file location with the file name appended at the end, but this perpetually says there is, "No such file or directory at," pathToFile location. I'm at my wits end here and could use some help.
EDIT:
To answer questions, access isn't an issue. I am using a Mac and a Windows machine to access the file share without problems. Path looks like so:
//${ipAddressOfServer}/ParentFolder/ChildFolder/${fileName}

Related

How to obtain a relative path from device filesystem path?

I’m looking to allow a user to upload a javascript file containing a thread and then to spawn that thread, but I’m facing some issues with the device paths.
First I use the DocumentPicker to pick the file:
const pickerResult = await DocumentPicker.pickSingle({
type: [types.allFiles],
copyTo: 'documentDirectory'
});
Then I use the RNFS.readDir to get the file and its path comes back as /data/user/0/playground.com/files/my-thread.js.
Now, the react-native-threads expects a relative path like such new Thread('./worker.thread.js’);. If I try to pass the path received by RNFS I receive the following no such file error:
Error: ENOENT: no such file or directory, open '/data/user/0/playground.com/files/my-thread.js'.
I feel like this may be possible to avoid by using a Blob, but perhaps there is an easier way?

Node.js source code getting sent over to client

I'm building with Node.js locally and I came along a weird problem where if I typed localhost:8080/server.js, the whole server-side source code shows up in the browser (the server file's name is server.js). I guess what's happening is the server looks for server.js, finds itself in the directory and sends it over. That's a huge security risk right? Any way to solve it?
All my code files are present in the same folder; would changing that be the best way to fix the problem?
Since index.html and server.js are in the same folder then the server will send server.js when requested because it thinks it's javascript code for the client and not the server.
You should put the files you want to serve in sub folder (usually named public) like this:
public/index.html
public/style.css
then start the server using that folder with:
app.use(express.static("public"))

How to download files and store them to s3 by nodejs (through heroku)?

I want to know how to download the files to aws-s3 through heroku, and dosen't use the web page.
I'm using wget in Nodejs to download audio files, and wanting to re-encoding them by ffmpeg. Though I have to store the file first, but heroku doesn't provide the space to store.
Then I find the way that heroku can connect with aws-s3, however the sample code is the page for user to upload, and I'm just want to using codes.
This is the code that haven't connected with s3 yet.
const wget = spawn('wget',['-O', 'upload_audio/'+ sender_psid +'.aac', audioUrl]);
//spit stdout to screen
wget.stdout.on('data', function (data) { process.stdout.write(data.toString()); });
// //spit stderr to screen
wget.stderr.on('data', function (data) { process.stdout.write(data.toString()); });
wget.on('close', function (code) {
console.log(process.env.PATH);
const ffmpeg = spawn('ffmpeg',['-i', 'upload_audio/'+ sender_psid +'.aac', '-ar', '16000', 'upload_audio/'+ sender_psid +'.wav', '-y']);
...
});
And it comes the error:
upload_audio/1847432728691358.aac: No such file or directory
...
Error: ENOENT: no such file or directory, open 'upload_audio/1847432728691358.wav'
How can I solve the problem?
Could anyone give some suggest plz?
Maybe won't use s3 can get it?
I don't recommend using S3 until you've tried the NodeJS Static Files API. On the server, that looks like this:
app.use(express.static(path.join(__dirname, 'my_audio_files_folder')));
More on NodeJS's Static Files API available here.
AWS S3 seems to be adding unneeded complexity, but if you have your heart set on it, have a look at this thread.

Every JS file throws error until it's opened and saved on Azure

I'm deploying a node app to Azure, using local git deployment. I have it working without issues in the staging environment (also on Azure) and I'm now setting up a production environment.
Every single file involved in the app throws an error - but as soon as I open that file in my FTP client and just save it, without making changes, the error for that particular file goes away - and the next file used throws an error.
So the steps I took are something like this:
Run deployment, refresh browser.
Get error like Unexpected token ILLEGAL on server.js line 1
Save server.js in FTP client, without making changes.
Restart app, refresh browser.
server.js now has no issues, but the first file it requires, express, gives an error cannot find module ./lib/express on node_modules/express/index.js:11 (./lib/express is definitely there)
Save node_modules/express/index.js:11 in FTP client, without making changes.
Restart app, refresh browser.
Now, node_modules/express/index.js has no issues, but the first file it requires, ./lib/express will then give an error: cannot find module merge-descriptors on node_modules/lib/express.js:16
I'll stop there, but in real life I continued and the behaviour is consistently ongoing - each file errors on the first thing it tries to require, until it's been saved in the FTP client.
To top it all off, I left the app unchanged for 20 minutes, came back, and I was back to the beginning - with an Unexpected token ILLEGAL on server.js line 1 despite NO changes to my code. I tried saving each file and basically just repeated the steps above, getting the same results.
I'm completely stuck and have no idea what to do next short of saving every single file in the codebase. Any idea what could be going on here, or how I could move forwards with debugging the issue?
You most likely have a Byte-Order-Mark at the beginning of your files.
There is a simple gist by Domenic Denicola that shows how you can detect and remove this for all files in the current directory:
var fs = require("fs");
var path = require("path");
var wrench = require("wrench");
var BOM_CHAR_CODE = 65279;
var BOM = String.fromCharCode(BOM_CHAR_CODE);
var fileNames = wrench.readdirSyncRecursive(process.cwd()).filter(function (fileName) {
return path.extname(fileName) === ".js";
});
fileNames.forEach(function (fileName) {
fs.readFile(fileName, "utf8", function (err, fileContents) {
if (err) { throw err; }
if (fileContents.charCodeAt(0) !== BOM_CHAR_CODE) {
fs.writeFile(fileName, BOM + fileContents, "utf8", function (err) {
if (err) { throw err; }
});
}
});
});
After being in touch with Azure support, it turns out the issue was due to WEBSITE_DYNAMIC_CACHE being set to 1, not 0. The feature isn't visible in the azure portal, and is "in development and is currently in private preview." The issue I came across is a known bug with WEBSITE_DYNAMIC_CACHE and node apps.
We're still not sure how / why it got set to 1 in the first place, but it's fixed, so we don't care for now.
What a fun day!

Serving static files in node.js - 500 internal server error

I'm having a weird issue where our staging server is throwing a 500 error when trying to retrieve css or js assets. We are using broccoli to compile the assets to a distribution directory, so I have ~/dist/assets/app.css (as well as app.js and an img directory). Images seem to be served fine! Only the app.js and app.css files are throwing the 500 error. I've ensured the files definitely exist in their proper places.
We're using express.js and the serve-static module. Code is simply:
serveStatic = require('serve-static');
app.use(serveStatic('dist/assets'));
Then hitting 'http://url.com/assets/app.css' throws the 500.
Hitting 'http://url.com/app.css' WORKS. This seems like it would be okay (since I'm serving dist/assets so the request should be relative to /assets), but this was all working with the /assets prefix on the request a few days ago.
There is no error output produced in the logs anywhere. Stumped on this one.
I just want to make sure I'm not doing anything too dumb.
Have you tried this:
serveStatic = require('serve-static');
app.use(serveStatic('dist'));
serveStatic(root, options)
Create a new middleware function to serve files from within a given
root directory.
Based on that statement, you should expect that using "serveStatic('dist/assets')" will serve the app.css from http://url.com/app.css

Categories

Resources