Home > Web Front-end > Front-end Q&A > How to send pictures in nodejs

How to send pictures in nodejs

PHPz
Release: 2023-04-19 15:21:57
Original
1132 people have browsed it

Node.js is a very popular server-side JavaScript runtime environment that allows developers to use JavaScript language for server-side application development. This article will introduce how to send images in Node.js.

1. Using the HTTP module of Node.js

The HTTP module that comes with Node.js allows us to create and handle HTTP servers and clients. We can send images using this module. The following is a sample code:

const http = require('http');
const fs = require('fs');

http.createServer(function(req, res) {
  res.writeHead(200, {'Content-Type': 'image/png'});
  fs.readFile('image.png', function(err, data) {
    if (err) {
      res.writeHead(404);
      res.write("File not found");
    } else {
      res.write(data);
    }
    res.end();
  });
}).listen(8080, function() {
  console.log('Server listening on http://localhost:8080');
});
Copy after login

This code creates an HTTP server. When a request comes, it reads the local image.png file and sends it out as the content of the HTTP response.

2. Use third-party modules

You can use third-party modules to simplify the process of sending pictures. One of the popular modules is express. Here is an example:

const express = require('express');
const fs = require('fs');

const app = express();

app.get('/', function(req, res) {
  fs.readFile('image.png', function(err, data) {
    if (err) {
      res.writeHead(404);
      res.write("File not found");
    } else {
      res.writeHead(200, {'Content-Type': 'image/png'});
      res.write(data);
    }
    res.end();
  });
});

app.listen(8080, function() {
  console.log('Server listening on http://localhost:8080');
});
Copy after login

This example uses the express module to create an HTTP server that handles GET requests from clients and responds to image.png files.

3. Use Base64 encoding

Another method is to embed the image in the HTML response by using Base64 encoding. Here is a sample code:

const http = require('http');
const fs = require('fs');

http.createServer(function(req, res) {
  res.writeHead(200, {'Content-Type': 'text/html'});
  fs.readFile('image.png', function(err, data) {
    if (err) {
      res.writeHead(404);
      res.write("File not found");
    } else {
      const img = Buffer.from(data).toString('base64');
      res.write('<img src="data:image/png;base64,&#39; + img + &#39;"/>');
    }
    res.end();
  });
}).listen(8080, function() {
  console.log('Server listening on http://localhost:8080');
});
Copy after login

This example reads the image.png file into memory, then converts it to Base64 encoded format and embeds it in HTML to display the image on the client.

Summary

The above are the steps and sample code required to send images in Node.js. We can use the HTTP module that comes with Node.js to send images, or we can use third-party modules such as express. At the same time, we can also use Base64 encoding to embed images into HTML responses.

The above is the detailed content of How to send pictures in nodejs. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template