当前位置:网站首页>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
边栏推荐
- GaussDB(for MySQL) :Partial Result Cache,通过缓存中间结果对算子进行加速
- Jielizhi Bluetooth headset quality control and production skills [chapter]
- Windows installation WSL (II)
- leetcode96不同的二叉搜索樹
- 基于全志H3的QT5.12.9移植教程
- [embedded system course design] a single key controls the LED light
- Which app is better and more secure for stock mobile account opening
- Concurrentskiplistmap -- principle of table skipping
- Correlation - intra group correlation coefficient
- Resumption of attack and defense drill
猜你喜欢

USB-IF协会与各种接口的由来

UDS bootloader of s32kxxx bootloader
![Window sorting functions rank and deny for SQL data analysis_ rank、raw_ Number and lag, lead window offset function [usage sorting]](/img/3a/cced28a2eea9f9a0d107baabd01119.png)
Window sorting functions rank and deny for SQL data analysis_ rank、raw_ Number and lag, lead window offset function [usage sorting]
![[cmake] cmake configuration in QT Creator](/img/e3/1cf76f88eaddb5d32184523dfb049c.png)
[cmake] cmake configuration in QT Creator

mysql之B tree 以及 B+tree

【QT】测试Qt是否能连接上数据库

Leetcode96 different binary search trees

PyCharm调用matplotlib绘图时图像弹出问题怎么解决

GaussDB(for MySQL) :Partial Result Cache,通过缓存中间结果对算子进行加速

Ldr6035 smart Bluetooth audio can be charged and released (5.9.12.15.20v) fast charging and fast releasing device charging
随机推荐
. env. XXX file, with constant, but undefined
Which securities company is the best to open a stock account? Is there a security guarantee
SQL Server 安装指南
Is it safe and reliable to open an account in Caixue school and make new debts?
Asp .NetCore 微信订阅号自动回复之文本篇
Qt5.12.9 migration tutorial based on Quanzhi H3
[QT] QT cannot find a solution to the compiler using msvc2017
Is it safe to choose mobile phone for stock trading account opening in Beijing?
Deep learning | three concepts: epoch, batch, iteration
多表操作-一对一,一对多与多对多
Resumption of attack and defense drill
PWN attack and defense world cgpwn2
Is the securities account given by qiniu business school safe? Where can I open an account
MySQL: the difference between insert ignore, insert and replace
leetcode96不同的二叉搜索樹
Windows installation WSL (II)
.env.xxx 文件,加了常量,卻undefined
Graduation season | Huawei experts teach the interview secret: how to get a high paying offer from a large factory?
Concurrentskiplistmap -- principle of table skipping
PyCharm调用matplotlib绘图时图像弹出问题怎么解决