How to get the url resource path match in node.js express? - node.js

I want to get png image by path like this:
http://127.0.0.1/image/line.png
how do I write the app.get() function to match the path and get the filename, then read the file in location and return it?
I am new to express in node.js.

If you are using ExpressJS 4.0. You can do it by my example below:
app.get('/image/:fileName', function(req ,res) {
var path = require('path');
var file = path.join(PATH_TO_IMAGE_DIRECTORY, req.params.fileName);
res.sendFile(file);
});
Please make note that you should change PATH_TO_IMAGE_DIRECTORY by your image directory location. For example: __dirname, '../upload'
I shared a full post about upload and get the uploaded images with nodejs and expressjs 4.0: HOW TO UPLOAD FILES WITH NODEJS AND EXPRESSJS 4

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!

Cannot get image from node server public/image folder

I created a node server public/image folder to store my image file uploads. I can upload to the folder and store the url in the postgresql table with that users id with no problem. However, when I fetch that image url from the the database, I get the url but the browser always says "cannot get /...". Im using react to fetch the json response and add the image url property to the src in an avatar. Please help. I don't understand why. I'm using the app.use middelware below:
app.use(express.static('public')) app.use('/images', express.static('images'))
This is the image in the folder:
This is the avatar where I add the src props.
Here is the URL inside the browser react tools
in case you're getting a CORS error, you should use this:
var cors = require('cors')
const app = express()
app.use(cors())
app.use(express.static('public'));
you can access it with you're specified port:
http://localhost:PORT/filenameWithExtension

cannot reach a file on server though I exposed using express

In my project I need to store some text files dynamically and end user should be able to download them from browser. I know the best way is using object store like MINIO or S3 but unfortunately the only way I have is to use in memory storage. So what I am trying to do is: I made a public folder and I exposed it using the following code:
var express = require('express');
var app = express();
//setting middleware
app.use(express.static( 'public')); //Serves resources from public folder
var server = app.listen(5000);
it is as simple as that. Then for testing just to make sure I can download and reach a file I created a public folder with t.txt file in it and when I try:
http://localhost:5000/public/t.txt
I get
So why am I not getting it? Also is what I am trying to achieve will be a good match to scenario and is it doable at all?
When you're not specifying a path in app.use(), your app will serve the contents of the directory you're pointing to in express.static() on the root path. Try this:
http://localhost:5000/t.txt
That said, if you want it to be accessible at /public/t.txt, just specify the path:
app.use('/public', express.static('public'))
At first use the following line of code:
app.use(express.static(__dirname+'/public'));
This means that your home directory for static HTML pages is in the "public" folder. Note that the "__dirname" points to the directory of the current js file.
After that, call the following URL from the browser or POSTMAN:
http://localhost:5000/t.txt
As you can see, there is no need to write http://localhost:5000/public/t.txt referring to the "public" folder because you have already specified that in the app.use line.

how to code a sails http middleware for a directory?

i am using one npm package for generating QR code after it will save the image in qr directory and after i cant access the file with my app and also directly. i think it is the problem of middleware.(i can acces the images after sails restating, it is working on my localhost). each time a user using my app it will automatically generate qr for the user changes.
You have plenty of ways to do that.
Create a symlink in asset (eg: assets/app) pointing to your qrcode folder
You can access express trough sails.express. So you can add in your config/bootstrap :
var express = require('express');
sails.express.app.use(express.static(process.cwd() + '/qrcode_folder'));
Custom middleware. Add in your config/http.js :
var express = require('express');
module.exports.express = {
qrCodeMiddleware: function (app) {
app.use(express.compress());
app.use('/qrcode', express.static(process.cwd() + '/my_qrcode_folder'));
}
};

Resources