naive-ui/playground/uploadServer.js

34 lines
722 B
JavaScript
Raw Normal View History

2020-02-16 21:52:38 +08:00
const express = require('express')
const multer = require('multer')
const cors = require('cors')
const path = require('path')
const fs = require('fs')
2020-02-16 21:52:38 +08:00
const app = express()
const dest = path.resolve(__dirname, 'temp')
const upload = multer({ dest })
2020-02-16 21:52:38 +08:00
app.options('/upload-test', cors())
app.post(
'/upload-test',
cors(),
function (req, res, next) {
req.on('close', () => {
console.log('文件上传取消')
})
req.on('error', () => {
console.log('文件上传出错')
})
next()
},
upload.any(),
function (req, res, next) {
if (!fs.existsSync(dest)) fs.mkdirSync(dest)
2020-02-18 15:15:42 +08:00
console.log(req.headers)
console.log(req.body)
res.send('very good')
}
)
2020-02-16 21:52:38 +08:00
app.listen(3000)