How to serve an image using nodejs

前端 未结 11 1250
渐次进展
渐次进展 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:08

    It is too late but helps someone, I'm using node version v7.9.0 and express version 4.15.0

    if your directory structure is something like this:

    your-project
       uploads
       package.json
       server.js
    

    server.js code:

    var express         = require('express');
    var app             = express();
    app.use(express.static(__dirname + '/uploads'));// you can access image 
     //using this url: http://localhost:7000/abc.jpg
    //make sure `abc.jpg` is present in `uploads` dir.
    
    //Or you can change the directory for hiding real directory name:
    
    `app.use('/images', express.static(__dirname+'/uploads/'));// you can access image using this url: http://localhost:7000/images/abc.jpg
    
    
    app.listen(7000);
    

提交回复
热议问题