Error finding html file with fly - node.js

I am deploying my first website where I used nodejs in the backend. I am facing an issue for which I have not been able to find a solution for. The website is running perfectly localy and I deployed it a first time with heroku (free version), there was no problem at all.
I just paid for a vps so I would be able to deploy my application with flightplan. Everything works and the website is correctly deployed to the VPS, but when I try to access it with "ip-address:port" I get the following error:
Error: ENOENT: no such file or directory, open './views/base.html'.
Would anyone have an idea on how this happened?
The line that calls the file is:
var file = fs.readFileSync("./views/base.html", "UTF8");
res.status(200).send(file);
Thanks for your help !
Edit:
In the server.js file I use the following line to 'hide' the files in the frontend, could it have something to do with that?
app.use('/webapp', express.static(path.join(__dirname, '/views')));

Found the solution, I don't know why, but the path didn't seem to work properly. Anyway I used
var file = fs.readFileSync(__dirname + "/../views/login.html", "UTF8");
which worked fine

Related

Dockerfile setup for Deploying a Puppeteer Nodejs App on Droplet

I have exhausted all the probable solutions on stackoverflow and beyond all to no success.
My use case is a very simple nodejs app that uses puppeteer ^19.7.1.
My directory structure has the file .puppeteerrc.cjs with content
const { join } = require('path');
/**
* #type {import("puppeteer").Configuration}
*/
module.exports = {
// Changes the cache location for Puppeteer.
cacheDirectory: join(__dirname, '.cache', 'puppeteer'),
};
However, when the server starts I am constantly greeted with the error message:
/workspace/.cache/puppeteer/chrome/linux-1069273/chrome-linux/chrome: error while loading shared libraries: libnss3.so: cannot open shared object file: No such file or directory
Everything works well on my localhost. The issue only starts when I hosted it on Digital Ocean Droplets.
I tried copying over the Dockerfile setup here https://pptr.dev/troubleshooting#running-puppeteer-in-the-cloud as is over to the root of my project to see if the issue will be resolved all to no available.
So, please I would really appreciate if any one can help me with a working Dockerfile configuration to address this as I've spent all day on this to no success.
I intend to host the app on Digital Ocean's Droplets
Thanks in anticipation for your time

Issue with running PDF.JS

I am looking to create a simple program that prints the text contents of a pdf file to the screen using the library pdf.js (https://www.npmjs.com/package/pdfjs-dist)
So far, I have an index.js file that is calling let PDFJS = require('pdfjs-dist'); I installed pdf.js using via the command 'npm i pdfjs-dist'. Problem is, as soon as I tried to run my index.js file 'run index.js' I am hit with the following error
/path/to/node_modules/pdfjs-dist/build/pdf.js:1361
await this._transport?.destroy();
^
SyntaxError: Unexpected token '.'
I'd like to know if anyone else had faced a similar issue when using pdf.js (will provide link) and if so, how to fix it? is there something that I require before using pdf.js?
any help would be very helpful.

Serving static files with Express.js on heroku. Where do files reside in Heroku?

The issue is Heroku can not find the files of my node project. This prompts the 404 Not found error. The question is how does the generic node Heroku file structure look like? If so, where do the files of my project actually reside in the heroku?
The scenario:
With the express library, I put all files needed to serve a frontend template, in a folder called 'public'. The absolute path leading to 'public' is needed and'_dirname' is the variable that contains that.
app.use(express.static(_dirname+'/public');
Now, Heroku always makes a GET request to the root of the application. As a response I send the
index.html file as the starting template. I also provide its absolute path as '{root:_dirname}'.
app.get('/', function(req, res) {
res.sendFile('public/index.html'{root:_dirname});
});
When the server is run, I get a 404 Not found error. Which means Heroku cannot find the files.
Heroku root folder is named app/. So as expected, providing the local '_dirname' is wrong since the application is now running on Heroku server not locally.
2020-09-10T15:28:12.127567+00:00 app[web.1]: Error: ENOENT: no such file or directory, stat '/app/C:/Users/...
But, pointing at the public folder alone(only /public), assuming app/ is root, still prompts the 404 error not found.
2020-09-10T15:31:23.630358+00:00 app[web.1]: Error: ENOENT: no such file or directory, stat '/index.html'
The Question
Which leads to the question, what does heroku file structure look like? where do project files reside in the heroku file structure, so i can provide the correct path on the GET request?
My file structure
After Bergur suggestion:
It prompts a 404 Not found. However, the path is now theoretically correct.
This is what I changed:
app.use(express.static(path.join(__dirname+'/public')));
app.get('/', function(req, res) {
res.sendFile('index.html',{root:__dirname+'/public'});
});
The 404 error comes from the 'app.get('/,function(req,res){});'. Without this handler, my heroku application shows a 'cannot GET' message on the template.
Have you tried to log out the full path just to see if there's something off?
I have several heroku projects with nodejs and react/vue/angular dist folder without problems.
I would change your code a little bit to:
use nodes __dirname
Use path.join instead of inserting the '/' yourself. It might cause some problems.
So final version might look like:
app.use(express.static(path.join(__dirname, 'public')));
This assumes the project structure is
-index.js
-public/index.html
There should be no need to use sendFile, serving the public folder is enough. Just make sure that the public folder is being deployed/pushed to heroku.
If you share with us your folder/project structure we might help you you better.

Setting Up Node.js App Directory

I'm completely new to using Node.js and even utilizing the command line, so this question may be extremely elementary, but I am unable to find a solution.
I am trying to set up an app directory using Node.js and NPM. For some reason, whenever I try to use the port:5000 I get a "Cannot GET/" error. My question is, why is my setup for my app directory not working?
I have installed connect and serve-static, and yet it will not retrieve files and listen on port 5000. I have created a server.js file in my user, kstach1. Here is the code I have within that file:
var connect = require('connect');
var serveStatic = require('serve-static');
var app = connect();
app.use(serveStatic('../angularjs'));
app.listen(5000);
So, I don't quite understand why this won't reference my folder of angularjs, where I want to store my app. I have tested it by adding a file within the folder called test.html, and entered localhost:5000/test.html, and still get the "Cannot GET/test.html" error.
I know that Node is working correctly because I can enter scripts into the command line and they give the correct output. I do this as a user (kstach1).
The only thing I can think of that I may be doing wrong, is where my files are located. I have the angularjs folder located in the root user folder on my Mac (kstach1), as well as the server.js file. Is this incorrect? If this is not the issue, is it because of where Node is installed (usr/local/bin/node)? My research to this point has led me to think that my problem could also be that I need to add the installation directory to my path. However, I don't want to mess with this unless I know that is the case.
Any help would be greatly appreciated.
I did a little research on the serve-static package and copied the code you provided.
My project folder is located at "C:\teststatic" and the folder with the static files is: "C:\angularjs", also using "text.html" that is located in the 'angularjs' folder.
When running the code you provided and going to localhost:5000 it indeed returns "Cannot GET/". This is most likely because there is no "/" file declared.
Going to localhost:5000/test.html works for me, so you could try setting a "/" like this:
app.use(serveStatic('../angularjs', {'index': ['test.html', 'index.html']}));
And see if that works for you. If not, you should double check directory names / locations.
EDIT:
From reading the comment you posted: try this instead:
app.use(serveStatic('angularjs'));
I suggest moving your angularjs folder up into your main project's directory in a public/ folder. Its a pretty standard convention to have all of your static assets in public/. You can then use the path module to automatically resolve your path, inserting this where you have '../angularjs': path.join(__dirname, 'public').
So, your code would look like this:
var connect = require('connect');
var serveStatic = require('serve-static');
var app = connect();
var path = require('path');
app.use(serveStatic(path.join(__dirname, 'public'));
app.listen(5000);
And, your directory structure would look like this:
server.js
public/
angularjs/
test.html
You should then be able to use localhost:5000/angularjs/test.html to view your test.html

nodejitsu 400 Error: ENOENT, open '/opt/run/snapshot/package/images/tmp/72118-89rld0.png

I'm using nodejitsu to deploy a simple image upload program with express. In my code I've changed the default upload directory by following command
app.use(express.bodyParser({
uploadDir: __dirname + "/images/tmp"
}));
It's working fine on my localhost but when I'm using nodejitsu I'm getting this error
400 Error: ENOENT, open '/opt/run/snapshot/package/images/tmp/72118-89rld0.png.
Can anybody tell me how to make it work on nodejitsu as well? Actually I'm new to node as well as nodejitsu.
I had the same problem. Try to check directory at application start:
var fs = require('fs'),
upload = __dirname + "/images/tmp";
fs.exists(upload, function (exist) {
if (!exist) {
fs.mkdir(upload);
}
});
It was helpful for me, may be it would helpful for you.
make sure that directory /opt/run/snapshot/package/images/tmp/ exists. Otherways just mkdir those directory
At first check that the directory exist or not. If not then create it and follow the command
sudo jitsu deploy
I think the problem will be solved.

Resources