What path to be used for local files which are in public folder in Next js api function - node.js

I am using Jimp NPM module for image processing in NextJs api function which is to use a local font situated in public folder, This is working fine in development server but it is giving me errors when deployed to vercel, error says no such file or directory, I even tried using Jimp.FONT_SANS_64_BLACK but still it is giving me the same error.

Related

Why I am not able to access my index html file when using npm scripts to initiate server?

I am using static middleware function to access HTML file with my express HTTP server. It works fine when I deliberately run the server.js file through node.js command line runnning in the same directory. However after using npm scripts it's not able to locate my assets whether its a HTML or any other media files.
Let me know what you think on these screenshots attached-
<https://i.stack.imgur.com/NxsfW.png>
<https://i.stack.imgur.com/xjbsx.png>
<https://i.stack.imgur.com/O2au5.png>
<https://i.stack.imgur.com/w8zIj.png>
<https://i.stack.imgur.com/po4vY.png>
<https://i.stack.imgur.com/hmB8A.png>

NodeJS AWS Lambda Require not able to import subfolders

So I am attempting to deploy a zipped nodejs project to AWS Lambda to run the script for me. It has a directory structure similar to the following:
apps
-api1
-case1
-index.js
-case2
-api2
-case1
index.js
And you get the rest. The lambda can successfully find and run the index.js script, the problem is I import those api modules through a require('./api1/case1);. For some reason, the lambda is unable to find them and when I try to test it throws this error:
"errorMessage": "Error: Cannot find module './apps/api1/case1'\nRequire stack:\n- /var/task/index.js\n- /var/runtime/index.mjs",
I've opened the zip I've uploaded and seen that the apps folder and everything in it is there in the correct spot, including the index.js file. Does anyone have any ideas what could be wrong?
I had this issue where I was zipping the entire folder which contains app folder and index.js file.
So after uploading, it was unable to locate.
instead, I selected all folders manually and compressed it. (I'm not compressing the root folder, I'm inside root folder and selecting all the required files and zipping it. ) and then uploaded.
Boom!!! it worked.

How to path to an image file in an electron app?

I am creating an electron app and packaging for distribution with electron-builder for windows and Mac. This app creates a folder and some pdfs inside varying based on user input. The pdfs also use an image, which as a node app I was keeping in the root folder of the app.
I managed to write to the desktop using an absolute path.
if (!fs.existsSync(`/Users/${user}/Desktop/2019 Certificates`)){
fs.mkdirSync(`/Users/${user}/Desktop/2019 Certificates`);
}
but when I use this relative path
stampandseal.png
I get the following error:
I expect it to find the png relative to the js file, however I get the following error:
fs.js:121 Uncaught Error: ENOENT: no such file or directory, open 'stampandseal.png'
If I understand your issue correctly, you are trying to copy an image from within the app bundle to the user's desktop. Use __dirname to reference the directory your code is executing in, and then build the path off of that.
The code below is used by my main.js file, which is in the directory containing my app directory. I use upath to build the path and jetpack instead of fs for copying
var fromPath = upath.join(__dirname, "app", "assets", "image.png");
jetpack.copy(fromPath, toPath, { overwrite: true });

Express App on Azure cannot load static file, status 500

I am deploying an Node.js Express App on Azure and the app failed to load static css/js file. The static file works fine under my localhost:3000. But when running on Azure, it throw out Internal Server Error 500.
The error in my chrome looks like:
GET https:///resources/css/style.css net::ERR_ABORTED 500 (Internal Server Error). The directory tree for this css file is /public/resources/css
I assume that my static file path is correct, otherwise I would see a 404 error instead of 500
app.use(express.static(path.join(__dirname, 'public')));
Per my experience, I think the incorrect deployment cause the error code 500 of Internal Server Error in Node.js on Azure. So to fix it, please first check the content of your web.config file in the path site/wwwroot. And there is a standard sample web.config from Kudu wiki page Using a custom web.config for Node apps which you can refer to.
If your Node app deployed successed, you will see a 404 Not Found error code for a non-exist static file instead of 500 as you said.
However, there is a difference for the variable __dirname between local and Azure. On local, the value of __dirname variable is always the full path name where you start up a node process, such as print __dirname will return D:\home if run node in CMD at D:\home. But on Azure, you will see the __dirname value in any path which be . if you try to run the command node -e "const path = require('path'); console.log(__dirname);" in the Kudu console, as the figure below.
Therefore, the path.join(__dirname, 'public') value is always public which not be related to the current path of node started up by IIS. So to fix it, please use an absolute path like D:\home\site\wwwroot instead of __dirname variable in path.join method or just directly use D:\home\site\wwwroot\public in express.static method.
Solved!
I checked the DetailedErrors log files from Azure Diagnostic Tool. And it was showing the 500 error was due to the following:
HTTP Error 500.0 - Internal Server Error
D:\Python34\python.exe - The FastCGI process exited unexpectedly
In fact, in my Azure App Service application setting, I turn on the python 3.4 version and which somehow azure trying to handle my static request using python. I simply turn off the python version in my application setting and now I can successfully load my static file.

express cannot handle file extensions in URL

I am using express with webpack-dev-middleware and webpack-hot-middleware in my development environment. I build a storage/DFS module that works in a similar way as GitHub. You paste a URL and it opens up a folder or a file. The problem now that I have is, when I paste a URL that contains a file extension at the end as in (/storage/folder1/folder2/file.py) I get 'Cannot GET /storage/folder1/folder2/file.py'.
It is a VUE app and for routing I am using vue-router. The path configured in VUE app is:
path: 'storage/*'
Using as a template for VUE app: https://admin.vuebulma.com/#/
Any suggestions on how fix this?

Resources