How to bring an image of a site in the Node API request?

0

I'm making an application and wanted to give a request that the API bring an image of that site in response. For example, giving the 404 error would like you to bring this .

How can I do this?

I currently have a method like this:

app.use((req, res, next) => {
    const error = new Error('Não há nada aqui :c');
    error.status = 404;
    next(error);
});

app.use((error, req, res, next) => {
    res.status(error.status || 500);
    res.json({
        error: {
            mensagem: error.message
        }
    })
});
    
asked by anonymous 21.12.2018 / 18:27

1 answer

1

The only way I found this was:

app.use((error, req, res, next) => { 
    res.status(error.status || 500); 
    res.send('<img src="http://http.cat/${error.status||500}" />'); 
});

That is, it returns a <img> element, having the src attribute with the link of the site image, according to the error code.

If you want, you can add style to the element, letting the image fill the entire screen:

res.send('<img src="http://http.cat/${error.status||500}" style="width:100%;" />'); 

res.sendFile would not be appropriate for this occasion

res.sendFile only works to send files that are on the same server. And so, I would not be able to add the image link as a file path, as it would return a "non-existent file" error.

I hope I have helped!

    
21.12.2018 / 20:12