readFileSync throws error when server launched as linux service - node.js

i'm trying to make a simple api for myself using a node/express server running on digital ocean. in the server file i have something like this:
var data = fs.readFileSync('path/to/data.json','utf8');
which works perfectly fine when i launch the server manually from the cmd line
node server
but what i have setup is a linux service so that everytime i restart my digital ocean machine it will automatically launch the server, the service ( kept in etc/init/ ) looks like this:
start on filesystem and started networking
respawn
exec node /path/to/server.js
the issue is that when I make the request to the server that runs the readFileSync call it works fine if the server had been launched manually from the cmd line, but when the server was launched via the service then the readFileSync throws the following error:
Error: ENOENT, no such file or directory 'path/to/data.json'
at Error (native)
at Object.fs.openSync (fs.js:500:18)
at Object.fs.readFileSync (fs.js:352:15)
the file and the directory do exist ( if i make a request for the data.json file directly in my browser i can see it )
what am i missing? is there something about launching the server as a service that conflics with using readFileSync? is there an alternative approach to what i'm trying to do? should i use some kind of request/fetch resource module for accessing that json file?

You're using a relative path but the process is not being started from where you think it is. Instead of using relative paths, use absolute paths.
So if your layout looks like:
server.js
path/
to/
data.json
Then inside your server.js, you can just do something like:
var path = require('path');
// ...
var data = fs.readFileSync(path.join(__dirname, 'path/to/data.json'), 'utf8');

Related

File path not found when code is deployed on a remote server

I have the following directory structure for my node/express app:
/
/client_side_code
- object00.png
/server_side_code
- server.js
When running a local server on port 3000, my server.js script accesses object00.png successfully with the following code:
var pathToImage = __dirname + "/../client_side_code/original_object_images";
fs.readdirSync(pathToImage).forEach(function(file,e) {
console.log(file);
});
Now, when I upload my application to a remote server (Heroku), the console says that this path can't be found. Specifically, I get the following error message in the console:
Error: ENOENT: no such file or directory, scandir '/app/server_side_code/../client_side_code/original_object_images
As you can see, the object00.png image can no longer be found. I thought using __dirname would also work on a remote server since it begins a relative path. Any thoughts as how I can allow this image to be accessed when it's hosted on a remote server?

Where will file be saved in heroku for node.js app?

I am create a file from a buffer and saving it to my heroku virtual machine:
var wstream = fs.createWriteStream( name + '.wav');
wstream.write(buffer)
wstream.end()
I need to send that file to google cloud storage using its absolute path: Do you know what the path will be?
path = '?'
The application root on Heroku should be /app. So if you have a file named foo.txt in the root of your repo, it would be /app/foo.txt. You can confirm this by opening up a console (e.g. heroku run bash) and running pwd.

Local node process running in wrong directory

I am using PHP storm 9 to run a node instance on my local dev environment., but the process seems to be running in server.js parent directory.
My folder structure looks like
app/
app_server/
server.js
user_resources/
user_resources/
When i write a file with in my local instance to writes user_resources in app/ and when i run the same process on live environment it writes to user_resources in app_server/
pdf.create(html, options).toFile(path+filename, function (err, reuslt) {
callback();
});
using fs writeFile, readFile or readdir gives similar behavior
Local node server i ran with PHPstorm and live server runs with forever
Both local and live is a ubuntu system.
Any suggestings to why local node seems to be running in server.js parent directory.
The node server is probably executed from the app directory by PHPStorm, while the live process runs from app/app_server.
If no other hint is provided in the server code on where exactly to put the user_resources it will reside within the current working directory (which is the path from where the node process was involved).
You may want to specify a path relative to the location of the server.js, this can easily be done like this:
var userResourcePath = __dirname + '/user_resources`;
Node always ensures the __dirname to be set to the path of the file it is in.
I made the assumption the user_resources path of your live environment (app/app_server/user_resources) is the one you want for local development.

Saving file on linux with FileStream of node js

I wan't to save pdf file on meteor server using NodeJs FileStream:
var fs = Npm.require('fs');
var writeStream = fs.createWriteStream(fileName);
pdf.create(html).toStream(function(err, stream) {
stream.pipe(writeStream);
});
There is no problem when I run code locally on Windows but after mup deploy of app to DigitalOcean server with Ubuntu it crashes my app.
I checked directory with ls -l and meteoruser is owner of directory I want to save file in, yet still it crashes my app.
Why is it crashing? Is there a way of checking what is the problem?
Edit:
I found that error I get is ENOENT.
Before piping stream I've tried to write some data to stream and it was success, so the problem must be:
stream.pipe(writeStream);
That gives ENOENT and crashes the server.

why I can't sendFile() in node.js express when deployed to AWS?

I am using node.js express to serve some static file like svg and json to the client, so I used sendFile() to send the files directly.
so here is my server file structures,
/root // the root of the server
/maps // put some static files
/routes/api // put the web API
in the web API
app.get('/buildings/map',function(req,res){
var mappath = 'maps/ARM-MAP_Base.svg';
res.sendfile(mappath);
})
It works perfectly on my local server to send files to the client, so it means the server could locate the file and send it. but when the server is deployed to the AWS, this methods would encounter a error - 242:Error: ENOENT, stat node.js, looks like it can't open the file in that path
I read some solutions like combining the __dirname with mappath, it didn't work since it would bring to the path of /routes/api/maps/...
so far I have no idea why it works on my local computer but fail to work on the AWS
Relative fs paths like mappath will be resolved from the current working directory, which isn't guaranteed to be consistent. It works locally because you're executing your application with /root as your working directory.
This is why you're finding recommendations to use __dirname, which an be used to resolve paths relative to the current script.
Though, along with it, you'll want to use ../ to resolve parent directories.
var mappath = 'maps/ARM-MAP_Base.svg';
res.sendfile(__dirname + '/../../../' + mappath);
This assumes the current script is located in and __dirname would be /root/maps/routes/api as the indentation in your directory tree suggests.

Resources