当前位置:网站首页>Node——Egg 创建本地文件访问接口
Node——Egg 创建本地文件访问接口
2022-07-02 00:15:00 【一只漫步前行的羊】
1.Router添加路由
router.get('/file/:fileName', controller.fileController.file)
2. controller文件创建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文件创建fileService.js
const Service = require('egg').Service;
class FileService extends Service{
async file(){
const {
ctx } = this;
let fileName = ctx.params.fileName //获取文件名
let suffix = fileName.split(".")[fileName.split(".").length - 1] //获取文件后缀
ctx.set('Content-Type', `image/${
suffix}`)
let filePath = `E:\\development\\myProject\\pc-dev-tool\\file\\` //文件目录
return fs.readFileSync(`${
filePath}${
fileName}`) //读取文件并返回
}
}
module.exports = FileService
访问: http://localhost:8080/file/1238127391.png
边栏推荐
- Operate database transactions with jpatractionmanager
- What is the purpose of ERP project implementation plan?
- 时间复杂度与空间复杂度
- When installing mysql, there are two packages: Perl (data:: dumper) and Perl (JSON)
- It's nothing to be utilitarian!
- Data analysis methodology and previous experience summary [notes dry goods]
- Jielizhi, production line assembly link [chapter]
- Use vb Net to convert PNG pictures into icon type icon files
- 正则表达式收集
- 回顾数据脱敏系统
猜你喜欢
PyCharm调用matplotlib绘图时图像弹出问题怎么解决
基于全志H3的QT5.12.9移植教程
LeetCode中等题题分享(5)
B tree and b+tree of MySQL
[Qt] résoudre le problème que Qt msvc 2017 ne peut pas Compiler
Selectively inhibiting learning bias for active sampling
[QT] solve the problem that QT MSVC 2017 cannot compile
Niuke - Practice 101 - reasoning clown
RPA教程01:EXCEL自动化从入门到实操
【QT】测试Qt是否能连接上数据库
随机推荐
[Qt] résoudre le problème que Qt msvc 2017 ne peut pas Compiler
mysql之B tree 以及 B+tree
UDS bootloader of s32kxxx bootloader
Jielizhi, production line assembly link [chapter]
The difference between timer and scheduledthreadpoolexecutor
[cmake] cmake configuration in QT Creator
Digital transformation has a long way to go, so how to take the key first step
牛客-练习赛101-推理小丑
Leetcode 96 différents arbres de recherche binaires
Cmake engineering related
Dongge cashes in and the boss retires?
【CMake】Qt creator 里面的 cmake 配置
Selectively inhibiting learning bias for active sampling
Which app is better and more secure for stock mobile account opening
Difficult to get up syndrome (bit by bit greed)
Ldr6035 smart Bluetooth audio can continuously charge and discharge mobile devices
Practical calculation of the whole process of operational amplifier hysteresis comparator
Comprehensive usage and case questions of sub query of SQL data analysis [patient sorting]
二叉搜索树的创建,查找,添加,删除操作
EMC circuit protection device for surge and impulse current protection