upload image

This commit is contained in:
KUN1007 2023-06-14 17:48:02 +08:00
parent c7aa6f0af2
commit dba19ab154
7 changed files with 13 additions and 11 deletions

View file

@ -1,28 +1,31 @@
const express = require('express') const express = require('express')
const path = require('path')
const cors = require('cors') const cors = require('cors')
const bodyParser = require('body-parser') const bodyParser = require('body-parser')
const app = express() const app = express()
const staticPath = path.join(__dirname, '.')
// 使用cors中间件来允许跨域请求 // 使用cors中间件来允许跨域请求
app.use(cors()) app.use(cors())
app.use(bodyParser.json()) app.use(bodyParser.json())
app.use(express.static(staticPath))
const port = 10007 // 端口号 const port = 10007 // 端口号
// 用户路由 // 用户路由
const userRoutes = require('./routes/user') const userRoutes = require('./src/routes/user')
// 帖子路由 // 帖子路由
const topicRoutes = require('./routes/topic') const topicRoutes = require('./src/routes/topic')
// 登录路由 // 登录路由
const loginRoutes = require('./routes/login') const loginRoutes = require('./src/routes/login')
// 注册路由 // 注册路由
const registerRoutes = require('./routes/register') const registerRoutes = require('./src/routes/register')
// 上传图片路由 // 上传图片路由
const uploadImagesRoutes = require('./routes/upload-image') const uploadImagesRoutes = require('./src/routes/upload-image')
app.use('/kungalgamer', userRoutes) app.use('/kungalgamer', userRoutes)
app.use('/topic', topicRoutes) app.use('/topic', topicRoutes)

View file

@ -75,16 +75,15 @@ function saveFiles(req, time = 0) {
// 遍历所有上传来的图片 // 遍历所有上传来的图片
objForEach(files, (name, file) => { objForEach(files, (name, file) => {
console.log('name...', name) console.log(file)
console.log('file.name...', file.name)
// 图片临时位置 // 图片临时位置
const tempFilePath = file.path const tempFilePath = file.filepath
// 图片名称和路径 // 图片名称和路径
const fileName = genRandomFileName(file.name || name) // 为文件名增加一个随机数,防止同名文件覆盖 const fileName = genRandomFileName(file.originalFilename || name) // 为文件名增加一个随机数,防止同名文件覆盖
console.log('fileName...', fileName)
const fullFileName = path.join(storePath, fileName) const fullFileName = path.join(storePath, fileName)
console.log('fullFileName...', fullFileName)
// 将临时文件保存为正式文件 // 将临时文件保存为正式文件
fs.renameSync(tempFilePath, fullFileName) fs.renameSync(tempFilePath, fullFileName)
// 存储链接 // 存储链接