简体   繁体   English

res.set('Content-Type','image/jpg') 在节点 js 中提供图像时不起作用

[英]res.set('Content-Type','image/jpg') is not working while serving image in node js

res.set('Content-Type', 'image/jpg') is not working while serving us files in node js. res.set('Content-Type', 'image/jpg') 在节点 js 中为我们提供文件时不起作用。 Please take a look at code what is wrong with this code.请看一下代码,这段代码有什么问题。 I am getting the User Value and avatar binary value from the database but not able to see it on the browser.我从数据库中获取用户值和头像二进制值,但无法在浏览器上看到它。

http://localhost:3000/users/5f8930bba34fd3166c1b3f72/avatar and I am hitting this URL on the local server http://localhost:3000/users/5f8930bba34fd3166c1b3f72/avatar 我在本地服务器上点击了这个 URL

router.get('/users/:id/avatar', async (req,res)=>{
        try {
            const user= await User.findById(req.params.id)
            if (!user || user.avatar){
                throw new Error('Avatar not found !!')
            } 
            res.set('Content-Type','image/jpg')
            res.send(user.avatar)
        } catch (error) {
            res.status(404).send()
        }
    })

Below are my dependencies:以下是我的依赖项:

"dependencies": {
    "bcryptjs": "^2.4.3",
    "express": "^4.17.1",
    "jsonwebtoken": "^8.5.1",
    "mongodb": "^3.6.2",
    "mongoose": "^5.10.8",
    "mongoose-unique-validator": "^2.0.3",
    "multer": "^1.4.2",
    "validator": "^13.1.17"
  }

This is my user model这是我的用户模型

const userSchema=new mongoose.Schema({
    name:{
        type:String,
        required:true,
        trim:true,
        //unique:true,
    },
    age:{
        type:Number,
        default:0,
        validate(value){
            if(value<0){
                throw new Error('Age must be positive number')
            }
        }
    },
    email:{
        type:String,
        required:true,
        trim:true,
        unique:true,
        lowercase:true,
        validate(value){
            if(!validator.isEmail(value)){
                throw new Error('Email is invalid')
            }
        }
    },
    password:{
        type:String,
        required:true,
        minlength:7,
        trim:true,
        validate(value){
            if(value.toLowerCase().includes('password')){
                throw new Error('Password can not contain "password"');
            }
        }
    },

    tokens:[{
        token:{
            type:String,
            required:true
        }
    }],

    avatar:{
        type:Buffer
    }
    
},{
    timestamps:true
})

使用res.setHeader()

res.setHeader('content-type', 'image/jpg');

no relation between res.set('Content-Type', 'image/jpg') and showing picture on the browser. res.set('Content-Type', 'image/jpg') 和在浏览器上显示图片之间没有关系。

first you should add avatar .. if you using postman make new request with Post avatar then you can able to see it in the browser首先你应该添加头像 .. 如果你使用邮递员使用 Post avatar 发出新请求,那么你可以在浏览器中看到它

hint: you have an error in if condition it should be : if (!user || !user.avatar) instead of if (!user || user.avatar)提示:如果条件应该是错误:if (!user || !user.avatar) 而不是 if (!user || user.avatar)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM