با سلام
بنده کد مربوط به ذخیره عکس و نمایش آن را در لیست را زدمو دیتای باینری عکس را در لیست مشاهده میکنم ولی زمانی که میخوام عکس را روت کنم و در آدرس مربوطه ببینم ، عکس را به من نمایش نمیدهد.
index.js اصلی
import configs from '../../configs/server.js';
import {sequelize} from '../models/index.js';
import express from 'express';
const app = new express();
import router from '../routes/contacts.js';
import bodyParser from 'body-parser';
import loggerMiddleware from '../middleware/logger.js';
import imageRouter from '../routes/images.js';
app.use(bodyParser.urlencoded({extended:false}));
app.disable('etag');
app.use('/contact' , router);
app.use('/images' , imageRouter);
app.use(loggerMiddleware);
try{
await sequelize.sync({alter:true});
console.log('All models were synchronized successfully.');
}catch(error){
console.log('error in sync' , error);
};
app.get('/' , async(req , res)=>{
res.send('hello from simple server :)')
});
app.listen(configs.port , () =>{
console.log('ContactListSequelize is running');
});
فایل routes\image.js
import express from 'express';
import { getContactProfilePicture } from '../controller/contacts.js';
const imageRouter = express.Router();
imageRouter.get('/profile-pictrure/:id' , getContactProfilePicture);
export default imageRouter;
فایل contoller\contacts.js
import multer from 'multer';
import { where } from "sequelize";
const upload = multer({ storage:multer.memoryStorage()});
export async function getContactProfilePicture(req, res) {
try {
const { profilePicture } = await contact.findOne({
attributes: ['profilePicture'],
where: { id: req.params.id },
});
res.type('image/jpeg');
res.send(profilePicture);
} catch(error) {
res.status(500).send({
message: 'Something went wrong',
error,
});
}
}
export async function getcontacts(req,res) {
const contacts = await contact.findAll({
attributes : [
'id',
'firstName',
'lastName' ,
'mobilePhone',
'isFavorite',
'profilePicture'
]
});
const normalizedContact = contacts.map(({ dataValues: { id, profilePicture, ...rest } }) => ({
id,
profilePicture: profilePicture ? `/images/profile-picture/${id}` : null,
...rest
}));
res.json(normalizedContact);
};
async function createContactCTL(req,res) {
try{
const {firstName , lastName , mobilePhone , isFavorite} = req.body;
const {buffer:profilePicture} = req.file || null;
const {id} = await contact.create({
firstName,
lastName,
mobilePhone,
isFavorite,
profilePicture,
});
res.send(`The Contact # ${id} ${firstName} ${lastName} is created`);
}catch(error){
res.status(400).send('somthing went wrong');
error;
}
};
export const createContact = [
upload.single('profilePicture'),
createContactCTL,
];
لطفا راهنمایی بفرمایید