From dba19ab15409c63be012895d8a790b0195d31ba6 Mon Sep 17 00:00:00 2001 From: KUN1007 Date: Wed, 14 Jun 2023 17:48:02 +0800 Subject: [PATCH] upload image --- server/index.js | 13 ++++++++----- server/{ => src}/routes/login.js | 0 server/{ => src}/routes/register.js | 0 server/{ => src}/routes/topic.js | 0 server/{ => src}/routes/upload-image.js | 11 +++++------ server/{ => src}/routes/user.js | 0 server/{ => src}/util.js | 0 7 files changed, 13 insertions(+), 11 deletions(-) rename server/{ => src}/routes/login.js (100%) rename server/{ => src}/routes/register.js (100%) rename server/{ => src}/routes/topic.js (100%) rename server/{ => src}/routes/upload-image.js (89%) rename server/{ => src}/routes/user.js (100%) rename server/{ => src}/util.js (100%) diff --git a/server/index.js b/server/index.js index bce7e232..acd6c2d6 100644 --- a/server/index.js +++ b/server/index.js @@ -1,28 +1,31 @@ const express = require('express') +const path = require('path') const cors = require('cors') const bodyParser = require('body-parser') const app = express() +const staticPath = path.join(__dirname, '.') // 使用cors中间件来允许跨域请求 app.use(cors()) app.use(bodyParser.json()) +app.use(express.static(staticPath)) 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('/topic', topicRoutes) diff --git a/server/routes/login.js b/server/src/routes/login.js similarity index 100% rename from server/routes/login.js rename to server/src/routes/login.js diff --git a/server/routes/register.js b/server/src/routes/register.js similarity index 100% rename from server/routes/register.js rename to server/src/routes/register.js diff --git a/server/routes/topic.js b/server/src/routes/topic.js similarity index 100% rename from server/routes/topic.js rename to server/src/routes/topic.js diff --git a/server/routes/upload-image.js b/server/src/routes/upload-image.js similarity index 89% rename from server/routes/upload-image.js rename to server/src/routes/upload-image.js index 3815afdb..ad278977 100644 --- a/server/routes/upload-image.js +++ b/server/src/routes/upload-image.js @@ -75,16 +75,15 @@ function saveFiles(req, time = 0) { // 遍历所有上传来的图片 objForEach(files, (name, file) => { - console.log('name...', name) - console.log('file.name...', file.name) + console.log(file) // 图片临时位置 - const tempFilePath = file.path + const tempFilePath = file.filepath // 图片名称和路径 - const fileName = genRandomFileName(file.name || name) // 为文件名增加一个随机数,防止同名文件覆盖 - console.log('fileName...', fileName) + const fileName = genRandomFileName(file.originalFilename || name) // 为文件名增加一个随机数,防止同名文件覆盖 + const fullFileName = path.join(storePath, fileName) - console.log('fullFileName...', fullFileName) + // 将临时文件保存为正式文件 fs.renameSync(tempFilePath, fullFileName) // 存储链接 diff --git a/server/routes/user.js b/server/src/routes/user.js similarity index 100% rename from server/routes/user.js rename to server/src/routes/user.js diff --git a/server/util.js b/server/src/util.js similarity index 100% rename from server/util.js rename to server/src/util.js