How to upload to Gridfs without express in Node.js - node.js

I am looking for a way to upload a pdf to gridfs, but I don't want to use Express. I want to upload a pdf that is created on the backend and simply upload it into gridfs. By this I mean I want to send the file directly to the database because the file is already created.
I've seen many articles (ex: https://hevodata.com/learn/mongoose-gridfs/) that describe using an Express endpoint to upload the file like this:
app.post('/upload', upload.single('file'), (req, res) => {
res.redirect('/');
});
This is not what I want. I want to send an already created pdf file to gridfs so that I can store it in my database. Does anyone know of a way to do this?

Related

How to create file uploading web service using node js with jwt Authentication?

I want to create file uploading web service with jwt Authentication such that peoples can upload there files to /upload route and I will use cloudinary service in my server to actually store them. User can also get their files using filename with /getfile route. Is there something else I should add in my project?
In order for the express server to be able to take a file as an input I would use the package Express file-upload in order to achieve this. The docs are super simple for this one.
Inside of your server.js file:
// setting temp files location
app.use(fileUpload({
useTempFiles : true,
tempFileDir : '/tmp/'
}));
upload route:
app.post('/upload', function(req, res) {
console.log(req.files.foo); // the uploaded file object
res.download('/report-12345.pdf')
});
This file obeject caqn then be used to save the file wherever you desire.
For sending the file back to the user Express also has a feature for this which is documented here
getfile route:
app.get('/getfile', function(req, res) {
// whatever file to be downloaded here
res.download('/report-12345.pdf')
});
Hope that's what you were asking for. Thanks!

How can I build a web app that I can send files to for further manipulation?

I want to build a NodeJS server that accepts a .wav file (1Mb) sent to its single endpoint, then changes the file through AudioContext API and then sends back the response with the result?
The server shouldn't store anything, so, no database required.
How can I achieve this? (or, please correct me if don't understand how things work)
I would do this with express: https://expressjs.com/
and as middleware add express-fileuplaod: https://www.npmjs.com/package/express-fileupload
app.post('/upload', function(req, res) {
console.log(req.files.foo); // the uploaded file object
});
instead of the console.log(); you'd make a readable stream / buffer and then use it in the
AudioContext API
here is also a interesting Article explaining to use this:
https://www.russellgood.com/process-uploaded-file-web-audio-api/

How to send an email from a form such as a contact form, with attachments

Im making a web app, using node.js, express.js and mongodb and trying get my contact form done. I would like site visitors to contact me via the form where they can put their name, email and message, along with an uploadable file image so they can show me what they want to be done as a sample through an image.
It seems like every websites have this functionailiry but I cant seem to find the answer Im looking for. Any help and guides would be appreciated.
i think you could use this library i've just found for uploading files with Javascript by using a file dialog: https://fineuploader.com/demos.
I think your issue is pretty much solved after that because for what concerns the form, you may easily create the input tags in HTML and then get data from them by using NodeJS.
To upload files you need to use a form-data handler. I use multer:
npm i multer
You upload a file like this:
const multer = require('multer')
const upload = multer({ dest: 'uploads' })
app.post('/profile', upload.single('image'), (req, res)) {
//Now the image is uploaded in uploads folder you just need to send it back
}
Note that image is a form's input name.
For infroamtion on how to send the image see this
Look also at the docs for multer
If you don't want to store image you can use then fs module to delete it.

How to attach file to email in Node.js using File Upload control

My Angular app sends a FormGroup to a Node.js server & then sends that form content via an Email.
At the moment, I can populate the email body like so:
<tr>
<th>Town</th><td>${content['formBody']['personalInfo']['town']}</td>
</tr>
and so on...
But, my form also has a File Upload control which I want to use to add file attachments to the email also.
Here is what I have at the moment:
<td>${content['formBody']['send']['fileUpload']}</td>
But instead of the actual selected file, "object Object" is appearing in the email body currently.
Is there a way I can attach the selected files to the email using the above approach, or is there a different way? Thanks a lot
What are you using in Node.js for getting the files?
A few months ago I needed to upload files and used Multer, a npm package for handling formdata files in node js. With it, you can use the files received on the backend and put it on the email.
In the frontend file
//Method to do the request
superagent
.post(/register")
.attach("avatar", uploadedImage)
uploadedImage has the image content that you get in the VueJS component
In the backend file
var multer = require('multer')
var upload = multer({ dest: 'uploads/' })
import fs from 'fs-extra'
router.post('/register', upload.single('avatar'), (req, res, next) => {
return fs.readFile(req.file.path)
.then(content => {
// The content of the file
})
}
For more information, here you have the answer Accessing data sent as FormData using Axios
Does it help?

How to accept a file and then store it in cloud storage

I am using expressjs (all newest versions of express, node, and npm). I have create a route such as this:
router.post("/", function(req, res, next) {
});
This route will need to be able to have a file (image/video/docx,etc) uploaded and needs to then be stored on a cloud storage service (Google Storage). I do not want to store anything on the server that express is running on, just want to receive the file and pass it on over to Google Cloud Storage. I see there are some libraries which do this in addition to express, but I couldn't not find how to do it using just express.
I think your clients might be able to upload directly to GCS by constructing a HTML form. Basically you can create a signed url and embed it in the form and then on submit, the upload goes straight to GCS and your app doesn't need to handle it at all.
See: https://cloud.google.com/storage/docs/xml-api/post-object

Resources