当前位置:网站首页>Node -- egg creates a local file access interface
Node -- egg creates a local file access interface
2022-07-02 00:20:00 【A walking sheep】
1.Router Add route
router.get('/file/:fileName', controller.fileController.file)
2. controller File creation fileController.js
const Controller = require('egg').Controller;
class FileController extends Controller{
async file() {
const {
ctx } = this;
let res = await ctx.service.fileService.file()
ctx.body = res
}
}
module.exports = FileController
3. service File creation fileService.js
const Service = require('egg').Service;
class FileService extends Service{
async file(){
const {
ctx } = this;
let fileName = ctx.params.fileName // Get the file name
let suffix = fileName.split(".")[fileName.split(".").length - 1] // Get file suffix
ctx.set('Content-Type', `image/${
suffix}`)
let filePath = `E:\\development\\myProject\\pc-dev-tool\\file\\` // File directory
return fs.readFileSync(`${
filePath}${
fileName}`) // Read the file and return
}
}
module.exports = FileService
visit : http://localhost:8080/file/1238127391.png
边栏推荐
- [embedded system course design] a single key controls the LED light
- Selectively inhibiting learning bias for active sampling
- Jielizhi, production line assembly link [chapter]
- 使用 ES 实现疫情地图或者外卖点餐功能(含代码及数据)
- Windows10 install WSL (I) (wslregisterdistribution error)
- It's nothing to be utilitarian!
- ThreadLocal内存泄漏是什么,怎么解决
- Node——Egg 创建本地文件访问接口
- Linux centos7 installation Oracle11g super perfect novice tutorial
- Timer和ScheduledThreadPoolExecutor的区别
猜你喜欢

Graduation season | Huawei experts teach the interview secret: how to get a high paying offer from a large factory?

Material design component - use bottomsheet to show extended content (I)

Windows10 install WSL (I) (wslregisterdistribution error)

Take the enclave Park as a sample to see how Yuhua and Shaoshan play the song of Chang Zhu Tan integrated development

2022拼多多详情/拼多多商品详情/拼多多sku详情

Relatively easy to understand PID understanding

Review data desensitization system

回顾数据脱敏系统

JS——图片转base码 、base转File对象

【QT】测试Qt是否能连接上数据库
随机推荐
Download the online video m3u8 tutorial
【CTF】bjdctf_2020_babystack2
cookie、session、tooken
SQL Server 安裝指南
使用 ES 实现疫情地图或者外卖点餐功能(含代码及数据)
创业团队如何落地敏捷测试,提升质量效能?丨声网开发者创业讲堂 Vol.03
多表操作-一对一,一对多与多对多
启牛商学院给的证券账户安不安全?哪里可以开户
It's nothing to be utilitarian!
数据分析方法论与前人经验总结【笔记干货】
Using SqlCommand objects in code
Node——生成微信权限验证配置
What is the purpose of ERP project implementation plan?
Halcon knowledge: an attempt of 3D reconstruction
mysql之B tree 以及 B+tree
MySQL: the difference between insert ignore, insert and replace
vs2015 AdminDeployment. xml
Comprehensive usage and case questions of sub query of SQL data analysis [patient sorting]
Relevant settings of wechat applet cache expiration time (recommended)
GCC compilation