Node.js express res.download and res.attachment don't changes filename - javascript

express 4.13
node.js 5.7.0
I am trying to make resposne to user with renamed file.
Archive.findById(req.params.id, function(err, file) {
res.download(file.path, "justfile.docx");
});
This code doesn't working. When i use it, file downloaded with default name, not with "justfile.docx".

Related

Error Enoent: no such file or directory multer

I was building a twitter clone app where users can upload tweets with and without images. Before deploying my backend, everything worked fine. Users could upload tweets with and without images. However, after I deployed my backend, I've been unable to get users to upload a tweet with an image. I keep getting this error.
Error Enoent: no such file or directory
This happened after I deployed my backend to Render. It was working fine just before I deployed my backend It is currently impossible to upload an image at the moment. I think this has something to do with the way I'm setting the path in my multer middleware. Here's the middleware I use to upload images:
import multer from "multer";
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, "uploads");
},
filename: function (req, file, cb) {
cb(null, new Date().toISOString() + file.originalname);
},
});
const uploadFile = multer({ storage }).single("image");
export default uploadFile;
As you can see, I'm setting the storage path to "uploads". However, since I've deployed my backend somewhere else, I think it should be updated. Can anyone tell me how I should update it if I already have a backend hosted somewhere else. If I'm wrong and it has nothing to do with how I'm setting the path, can you tell me what's wrong and how to fix it? Thanks a lot!

Can't return a xlsx file from a Node.js (express) service which gets it from a S3 bucket, for use by a browser app

Created a xlsx file in Excel, uploaded from desktop manually to AWS S3 via console.
Now want to add an endpoint to a Node.js express service that will retrieve that file from S3 and pass it back to a javascript browser application.
Seems fine with plain text file, but keep getting HTTP status code 500 for the xlsx file (and even csv created in Excel).
tried variations like:
res.send(s3.getObject().toString()); //this works with plain text file
res.send(s3.getObject().toString('utf-8'));
res.send(s3.getObject().toString('binary'));
no luck with any of these, call fails immediately with 500 Internal Server error.
Something like
s3.headObject(params,
(err, data)=>{
if (err) {
return res.status(500).end(err.message);
}
// Add the content type to the response (it's not propagated from the S3 SDK)
res.set({
'Content-Type': data.ContentType,
'Content-Length': data.ContentLength,
'Last-Modified': data.LastModified,
'ETag': data.ETag
});
s3.getObject(params).createReadStream().pipe(res)
}
);

Access a local directory to retrieve files from localhost and write back

I currently have an Angular app (MEAN Stack) that I am running locally on my Windows machine. Being new to Node/Express, I would like to be able to access a local directory from http://localhost:3006 that I have setup within my main app directory, called /myfiles which I am unsure how to do.
What I am unsure is, how do I create an endpoint to access and read these files from localhost within the /myfiles directory and display them within an Angular Material dialog?
Just not sure what I need to do as part of Express side (setting up the route) and then the Angular side (using HttpClient) to display.
Further to the above, I will also need to write back to the /myfiles directory, where I will need to perform a copy command based on a file selection within Angular.
You'll want to create an endpoint in Express that your Angular app can call to be served the files.
I'll assume the files you want to read and send are JSON files. Here's a really simple example of an endpoint that you can visit that will return the file to your frontend.
In your Angular code you will make a get call to /myfile
var fs = require("fs");
app.get('/myFile', (req, res) => {
var filepath = __dirname + '/myfiles/thefile.json';
var file = fs.readFileSync(filepath, encoding);
res.json(JSON.parse(file));
});
Then in Angular, you'll have something like
http.get('/myfile').subscribe( (data) => { console.log("The data is: ", data) });
ADDED
The example I provided above was just the basics to answer your question. Ideally, in production for file paths, you should the Node path library which 'knows' how to behave in different environments and file systems.

In which directory should I save uploaded files for MERN Stack?

I'm using the Mern stack, but I'm having an issue as to where to save uploaded files. Let me explain.
I have a form that sends over a formdata which includes a .jpg image.
On Node/express side, I receive it well. But now I'm stuck.
I'm using express-fileupload package which attaches an mv function that allows me to store my file in a directory.
In my endpoint, I have this snippet:
const img1= req.files.image1;
const img1Name = img1.name;
img1.mv("NOT-SURE-WHERE-TO-MAKE-MY-DIRECTORY"+img1Name, (err) => {
if (err) {
return res.status(500).json({message: 'Could Not mv file'});
} else {
return res.status(200).json({message: 'mv done'});
}
})
Where do I create the directory to store the .jpg image?
Do I create it in the client's src? or client's public?
Or, do I run the npm-run-build command to create my build folder and then point my mv function to save the file in there?
create a static file middleware in node app.js file to public directory then # public/images
If not try using multer it is much easier to upload Files

javascript how to get urls to files in folder from server?

I host a folder "/listings" on my jetty server.
I catch a file with a url to it
url = "/listings/file.json"
So i want to create an url like this for each file in this folder.
How can it be done in javascript?
If you have server-side language on the server, see Ajax and search for the server-side language methods to manage files, then use them to make changes in directories and files.
(with XMLHttpRequest you can run the server-side language file commands block)
Your question isn't very clear to me.
Assuming the files are already on your server and your Javascript runs in a Node environment (and not a browser), you can use Express to serve this directory:
var express = require('express');
var app = express();
app.use(express.static(__dirname + '/listings'));
app.listen(3000);
After that, you can access your files at `http://your_domain.com:3000/listings/file.json
Does this answer your question?

Categories