Codes/Javascript

How to show image, not download in nodejs express

2021. 2. 21. 14:22

 

 

const attachment = await AttachmentAPI.get_file_by_id(attachment_id);

/* ... */

const filename = attachment.filename;
const extension = filename.split('.').pop().toLowerCase();

/* ... */

res.contentType(`image/${extension}`);

/* ... */

try {
	const content = await fs.readFileSync(attachment.path);
	res.end(content);
} catch (error) {
	// Error Handling
}

 

Use res.end(), not res.send() OK?