1. 文件上传:
const express = require('express') const multer = require("multer"); const router = express.Router() const storage = multer.diskStorage({ // 设置保存文件的路径 destination: function (req, file, cd) { cd(null, './public/images') }, // 设置图片名称 filename: function (req, file, cd) { cd(null, `${Date.now()}-${file.originalname}`) } }) const upload = multer({ storage: storage }) router.post('/file', upload.single('file'), (req, res, next) => { let url = `/api/public/images/${req.file.filename}` if (!req.file) { return res.json({ code: 1, message: '上传失败' }) } else { return res.json({ code: 200, message: '上传成功', url: url }) } }) module.exports = router
2. 反显
const express = require('express'); const getImage = express(); const path = require('path') getImage.get('/public/images/*', (req, res) => { res.sendFile(path.resolve(__dirname, '..') + "/" + req.url); // res.send() }) module.exports = getImage