How to serve an image using nodejs

前端 未结 11 1290
渐次进展
渐次进展 2020-11-22 08:18

I have a logo that is residing at the public/images/logo.gif. Here is my nodejs code.

http.createServer(function(req, res){
  res.writeHead(200,          


        
11条回答
  •  醉酒成梦
    2020-11-22 09:14

    This method works for me, it's not dynamic but straight to the point:

    const fs      = require('fs');
    const express = require('express');
    const app     = express();
    
    app.get( '/logo.gif', function( req, res ) {
    
      fs.readFile( 'logo.gif', function( err, data ) {
    
        if ( err ) {
    
          console.log( err );
          return;
        }
    
        res.write( data );
        return res.end();
      });
    
    });
    
    app.listen( 80 );
    

提交回复
热议问题