Storing incoming files in a directory in Express JS - node.js

I am trying to configure ExpressJS to accept an incoming XML file for parsing. I have read that bodyParser has deprecated support for xml and I simply can't figure out how to make my application accept an incoming xml and store it.
Since the external server requires my site to be live to send the xml file, I have been building using local testing techniques. Namely, I created an upload form that will successfully do what I want it to do, upload an xml file and store it targeting a url.
When I push these changes to the server and target the same URL with the external server, the xml file gets lost in transit and is never stored.
Am I wrong in assuming that this POST coming from another server would automatically get placed into the faxes directory given this line?
In my Express Configuration
app.use(express.bodyParser( { keepExtensions: true, uploadDir: path.join(__dirname, '/faxes')}));

In express.js v 3+ the file upload middleware is removed you have to handle your file with your own .
Here is an example of formidable to upload file in a custom url path
var formidable = require('formidable')
app.post('/xmlpath' ,req, res, next){
var form = new formidable.IncomingForm();
form.keepExtensions = true;
form.parse(req, function(err, fields, files) {
var tempFilePath = files.file['path'],
userFileName = files.file['name'],
contentType = files.file['type'];
// then read your file with fs
// you can also move your file to another location with fs
// by default file will be place to tempFilePath
fs.readFile( tempFilePath, function(err, file_buffer){
// do what you want to do with your file
});
});
});

Related

file preview without downloading it using express

I'm trying to develop a file management web app and I want to add an option to preview files before downloading it I'm using Node Js express, is there any way to implement that.
or just a small guide because I tried all the possible solutions on the net but it's not working
Yes you can preview it, you just need to add declaration for your file path and you can access directly from url
const app = express();
const __dirname = path.resolve()
//public is the name of folder where your file is being stored
app.use("preview",express.static(path.join(__dirname,"public")));
So you can directly access the file using url expressurl/preview/filename and preview it wherever you want
You can use the built in node filesystem module fs to read and write to files on the server. So in your case you want to read the content of files, so something like this:
var fs = require('fs');
fs.readFile('my-file.txt', 'utf8', function(err, data) {
if (err) throw err;
console.log(data);
});

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 to create API that will force download file with HAPI JS

I'm working with NodeJS and using HAPI to create API for upload and download file. When uploading, I read the file information (filename, mime type and file content) and store it in database. File content is stored as base64 encoded string.
What I want to do is to create API, so when client hits it will be forced to download a file that is constructed based on the stored information using the code below
server.route({
method: 'GET',
path:'/file',
handler: function (request, reply) {
var fileData = // get file content;
var mime = // get file mime-type;
var fileBuffer = new Buffer(fileData, 'base64');
reply(fileBuffer)
.header('Content-disposition', 'attachment; filename=' + fileName)
.header('Content-type', mime).header('Content-length', fileBuffer.length).encoding('binary');
}
})
But this code looks like still not work, if I hit the API it will be loading process forever and no file downloaded. Anybody can give me suggestion on how to do it correctly?
UPDATE
the code is correct and works perfectly. the problem I had before is caused by another reason, incorrect encoding/decoding mechanism.
Check out the inert plugin for hapi which handles files, the repo is here

Generating in memory zip file at server and sending to client as download Node.JS

I'm trying to generate a zip file in memory on the server and sending it to the client as a download file.
Basically, there's a html page where the client types what file he wants.
The server receives what the client typed (via socket.io) and does a search on a mongodb. At this point, the server returns a link like this one
<a href='#' onclick='socket.emit("generateFile", id, path); return false;'>link</a>
Where id is the id of the entry in the database and path is the location of one of the files that will be in the zip.
After that, that server is supposed the create a json containing the entry that has that id and zip it along with some local files available on the server. My question is: how do I send this generated zip to the client? Remember: I want to send it without having to save it in the server's hdd. I tried using Express after the zip is created but the code doesn't reach this piece:
app.get('/', function(res, req)
{
res.set('Content-Type', 'application/zip')
res.send(generatedZipFile)
})
How to proceed?
This is from express-static-zip implementation
var contentType = mime.lookup(name);
if (contentType != 'application/octet-stream') {
res.set('Content-type', contentType);
var charSet = mime.charsets.lookup(contentType);
if (charSet) {
entryData = entryData.toString(charSet);
}
}
res.status(200).send(entryData);
You might want to set status code and mime format.

Express response body to buffer

I'm trying to build a quick and simple image uploading service with Node, that takes the received images and saves them to Mongo's GridFS.
GridFS get requires a Buffer object NodeJS Mongo Driver GridFS put
The question is pretty simple: how do I exactly cast/transform the received request body into a proper buffer.
My code so far (only the important pieces):
api.js
var express = require('express');
var connect = require('connect');
var app = module.exports = express.createServer();
app.configure(function(){
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
});
var upload = require('./upload.js');
app.post('/upload', upload.upload);
upload.js
exports.upload = (function(req, res, next){
console.log("Uploading image...");
// Create buffer
// Rest of the code
}
I've tried:
var buffer = new Buffer(util.inspect(req.body),'binary');
Creates the buffer, but it has a wrong size and probably not the correct content since util.inspect is obviously not the right way to go.
And:
var buffer = new Buffer(req.body);
Result:
[Decode error - output not utf-8][Decode error - output not utf-8]
Buffer length = 0
I'm quite new to both Node and JavaScript developing in general, so probably I'm missing something quite simple, don't hesitate to point the obvious :)
Thanks!
First, remember that Express is built on top of Connect, which is the library that handles a large amount of the lower-level HTTP work, and it's where bodyParser() comes from.
The body parser middleware internally uses Formidable to parse file uploads.
Formidable's default behavior is to write uploaded files directly to disk – in other words, you don't actually have access to the uploaded file stream within your route handler. You get the values of any regular form fields (<input>s) sent along in req.body, and you get uploaded file paths via req.files, but you don't get file content.
The easy answer here is to simply read the file from disk and use that to save into Mongo, remembering to delete the temporary file when done. Of course, this introduces the unnecessary intermediate step of writing the file upload to a temporary folder and then loading to Mongo.
If you want to stream file data directly into Mongo, you have more of a challenge in front of you. You'll have to write your own middleware to parse the upload stream.
This is actually relatively easy. You can just start with the internal Connect body parser implementation—using Formidable to do the heavy lifting—and use the onPart API to pass the stream back to your route handler so that you can pass it off to the Mongo driver.

Resources