cannot download files from custom folder in nestjs app - node.js

In my nestjs application am uploading files to custom folder "Uploads". But am getting error when am trying to access the file
when I call this URL :
http://localhost:3000/Uploads/file.png
{"statusCode":404,"message":"Cannot GET
/Uploads/file.png","error":"Not Found"}
But when I upload files to public folder am able to download it as:-
http://localhost:3000/file.png
How to download the files from Uploads folder?

Use this code in your main.ts
app.useStaticAssets(join(__dirname, '..', 'public'), {
prefix: '/Uploads/',
});

Related

React doesn't display images uploaded with Multer

After uploading an image with Multer to the uploads folder which is located in the root with server folder and client folder, React cannot access the images in the uploads folder, as the api/upload route returns just a string as /uploads/the_image_name.
I thought I should upload the images to the public folder in the React folder but I found that the convention is using an uploads folder in the root.
Server :
app.use('/uploads', express.static(path.join(__dirname, '/uploads')))
This line should probably solve it:
app.use("/uploads", express.static('uploads'))
You need to actually serve the images that reside in your uploads folder. One way to do this is to use the express static middleware. Assuming your uploads folder resides in your app's root, you'd simply add to your express app:
app.use("/uploads", express.static('uploads'))

app.sendFile() is not working and also __dirname is not working

i want to push app on heroku server but __dirname is set as 'app' which is my app.js file it is not folder and i am working in newapp folder
if(process.env.NODE_ENV==='production'){
app.use(express.static('my-app/build'));
app.get('*',(req,res)=>{
const index=path.resolve('my-app','build','index.html')
res.sendfile(index)
})
errenter image description hereor is:-{"title":"something went wrong","msg":"ENOENT: no such file or directory, stat '/app/my-app/build/index.html'"}
please open the above link ,link of photo of my all folder or file name

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 });

Running node (Express) server and dist folder / adding js folder

I have created server using nodeJS where I adding dist folder created by webpack.
server.js
app.use(express.static(path.join(__dirname, "dist")))
There is js folder also including all js files.
How can I add also js folder in correct way in server file?
Thanks in advance.
__dirname will return a relative path for your nodejs project.
You can print path.join(__dirname, "dist") in console and make sure that this folder is exists.

How can i use outside folder inside nodejs project

I am creating website using nodejs. i have lot of default js and css files from out of nodejs project file like assets. already i have created one public folder but i can not paste that folder inside that because that folder size is very big.how to call that files inside nodejs project.
folder structure:
assets
nodeproject
node_modules
public
views
index.js
package.json
package-lock.json
my assets folder have lot of css and html files like:
assets
1.style1.css
1.style2.css
1.style3.css
templatefolder:-
template1.html
template2.html
index.js:
const express=require('express');
const app=express();
app.listen(4600);
app.use(express.static('public'));
/*app.use(express.static('assets')); not working */
app.set('view engine','ejs');
app.get('/',(req,res)=>{
res.render("home");
});

Resources