J
J
jtag2017-09-01 13:53:30
JavaScript
jtag, 2017-09-01 13:53:30

How to pass a jpg image from Node.js to the browser?

How to pass a jpg image from Node.js to the browser and display it on the page?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
gleendo, 2017-09-01
@evgeniy8705

require("http").createServer((req, res) => {
  if (req.url === "/") {
    res.statusCode = 200;

    res.setHeader("Content-Type", "image/jpeg");

    require("fs").readFile("./image.jpg", (err, image) => {
      res.end(image);
    });
  }
}).listen(3000);

A
Alexander, 2017-09-01
@boratsagdiev

Raise a simple static server and voila - https://www.npmjs.com/package/node-static

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question