当前位置:网站首页>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
边栏推荐
- Cmake engineering related
- [CTF] bjdctf 2020 Bar _ Bacystack2
- Window sorting functions rank and deny for SQL data analysis_ rank、raw_ Number and lag, lead window offset function [usage sorting]
- Windows installation WSL (II)
- LDR6035智能蓝牙音响可充可放(5.9.12.15.20V)快充快放设备充电
- Jielizhi, production line assembly link [chapter]
- Niuke - Practice 101 - reasoning clown
- 求逆序数的三个方法
- 13 MySQL constraint
- Using multithreaded callable to query Oracle Database
猜你喜欢
B tree and b+tree of MySQL
数据分析方法论与前人经验总结【笔记干货】
【CMake】Qt creator 里面的 cmake 配置
Multi table operation - one to one, one to many and many to many
Leetcode medium question sharing (5)
多表操作-一对一,一对多与多对多
USB-IF协会与各种接口的由来
Use pair to do unordered_ Key value of map
Leetcode96 different binary search trees
leetcode96不同的二叉搜索樹
随机推荐
攻防演练复盘
Practical calculation of the whole process of operational amplifier hysteresis comparator
From 20s to 500ms, I used these three methods
Guide d'installation du serveur SQL
使用 ES 实现疫情地图或者外卖点餐功能(含代码及数据)
Linux CentOS7安装Oracle11g的超完美新手教程
[cascade classifier training parameters] training Haar cascades
启牛学院开户安全的吗?开户怎么开?
I want to ask, which is the better choice for securities companies? I don't understand. Is it safe to open an account online now?
js 公共库 cdn 推荐
Using multithreaded callable to query Oracle Database
Talents come from afar, and Wangcheng district has consolidated the intellectual base of "strengthening the provincial capital"
Accelerator systems initiative is an independent non-profit organization
在证券账户上买基金安全吗?哪里可以买基金
mysql之B tree 以及 B+tree
Ldr6035 smart Bluetooth audio can be charged and released (5.9.12.15.20v) fast charging and fast releasing device charging
【CTF】bjdctf_2020_babystack2
Timer和ScheduledThreadPoolExecutor的区别
UVM tutorial
What is ThreadLocal memory leak and how to solve it