-2
In my static file path, it finds the image but returns only its binary and not the image itself for the view.
What I want is to be able to view the image when placing in the browser for example: localhost:3000/files/imagem1.jpg
app.use('/files', express.static(__dirname + '/uploads'));
index.js of the API
var bodyParser = require('body-parser');
var express = require("express");
var cors = require('cors');
var path = require('path');
var app = express();
var router = require("./routes/routes");
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.urlencoded({extended: false}));
app.use(function(req, res, next){
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
res.setHeader("Access-Control-Allow-Headers", "content-type");
res.setHeader("Content-Type", "application/json");
res.setHeader("Access-Control-Allow-Credentials", true);
next();
});
// parse application/json
app.use(cors());
app.use(bodyParser.json());
app.use(express.json());
app.use('/files', express.static(__dirname + '/uploads'));
app.use("/api", router);
app.listen(3000, function(){ console.log('Servidor Web rodando na porta 3000') });
The return in the browser is the one of the image below. insert image description here
Important you [Dit] your question and explain objectively and punctually the difficulty found, accompanied by a [mcve] of the problem and attempt to solve. To better enjoy the site, understand and avoid closures and negativations worth reading What is the Stack Overflow and then the Stack Overflow Survival Guide (summarized) in Portuguese. On the print provided, it appears to be a properly formatted JPEG image (but served with the wrong content-type, the browser shows as text).
– Bacco
Thanks for the placement, I will go better in the next ones, that was my first question on the site. I have posted the solution, maybe I have lacked comment with details, but soon I will edit better.
– Thulio Xavier
If I understood your answer correctly, what might have made it work was taking the wrong header
res.setHeader("Content-Type", "application/json");
. It would be better to spell out image/jpeg (but it is an assumption based on what was provided)– Bacco
believe that on account of this excerpt " res.setHeader("Content-Type", "application/json");" all my returns were being in json, but I’m still not sure about it, so check other points I’ll better tailor the answer.
– Thulio Xavier
Anyway, I recommend a read in the past links to achieve a more effective return in the next, and to help compose the collection of the site.
– Bacco
Yes, I’ll take the time to read the links, thanks again.
– Thulio Xavier