当前位置:网站首页>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
边栏推荐
猜你喜欢

Dongge cashes in and the boss retires?

Review data desensitization system

4. Object mapping Mapstercover

Graduation season is both a farewell and a new beginning

LDR6035智能蓝牙音响可对手机设备持续充放电方案

Algolia's search needs are almost closed

Difficult to get up syndrome (bit by bit greed)

实例讲解将Graph Explorer搬上JupyterLab
![[QT] QT cannot find a solution to the compiler using msvc2017](/img/80/a4b17d6cc1ab6fe1366a3a3f43ec2e.png)
[QT] QT cannot find a solution to the compiler using msvc2017

Download the online video m3u8 tutorial
随机推荐
ADO. Net SqlCommand object
Example explanation: move graph explorer to jupyterlab
Ldr6035 smart Bluetooth audio can be charged and released (5.9.12.15.20v) fast charging and fast releasing device charging
Correlation - intra group correlation coefficient
Gaussdb (for MySQL):partial result cache, which accelerates the operator by caching intermediate results
SQL数据分析之窗口排序函数rank、dense_rank、raw_number与lag、lead窗口偏移函数【用法整理】
It's nothing to be utilitarian!
如何提升数据质量
Difficult to get up syndrome (bit by bit greed)
Vue force cleaning browser cache
【QT】Qt 使用MSVC2017找不到编译器的解决办法
PHP reads ini or env type configuration
Practical calculation of the whole process of operational amplifier hysteresis comparator
LDR6035智能蓝牙音响可对手机设备持续充放电方案
时间复杂度与空间复杂度
ERP项目施行计划的目的是什么?
How to improve data quality
求逆序数的三个方法
一个实习生的CnosDB之旅
Jielizhi, production line assembly link [chapter]