利用node+koa+axios實現(xiàn)圖片上傳和回顯功能
前言
開發(fā)前需要掌握的一些庫
koa:用來起一個web服務器
koa2-cors: 解決跨域問題
@koa/router: koa的路由處理
koa-body: koa參數(shù)的獲取
koa-static: 靜態(tài)內容
@koa/multer multer:圖片上傳的插件
代碼結構

實現(xiàn)代碼
1.第一步:用koa+koa-router搭建一個簡單的web服務
//main.js
const Koa = require('koa') // 引入koa
const Router = require('koa-router') // 引入koa-router
const koaBody = require('koa-body')
router.get('/', async (ctx) => {
ctx.type = 'html'
ctx.body = '<h1>hello world!</h1>'
})
app.use(router.routes())
.use(router.allowedMethods())
.use(koaBody())
// 啟動服務監(jiān)聽本地3000端口
app.listen(3000, () => {
console.log('應用已經啟動,http://localhost:3000')
})現(xiàn)在我們就可以打開http://localhost:3000看到 hello world
2.接著我們新建一個upload文件夾,且在代碼中加入靜態(tài)內容的的代碼
//mian.js 新增代碼
const serve = require('koa-static')
const path = require('path')
app.use(serve(path.join(__dirname, './upload')))此時假如你在upload文件夾下新增一張照片便可通過http://localhost:3000/***.png 查看到了。(***:你自己新增的照片名稱加后綴)
3.此時新增一個index.html文件,且加入以下代碼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<input type="file" class="file" name="avatar">
<button onclick="send()">上傳</button>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
let formData = new FormData()
document.querySelector('.file').addEventListener('change', function(e) {
let files = e.target.files
console.log(files)
if (!files.length) return
formData.append('file', files[0], files[0].name)
})
function send(){
axios.post('http://localhost:3000/upload',formData,{
Headers:{
"Content-type":"multipart/form-data"
}
})
}
</script>
</body>
</html>選擇圖片且上傳會發(fā)現(xiàn)存在跨域問題,那么發(fā)現(xiàn)問題解決問題直接上代碼:
//mian.js新增代碼
const cors = require('koa2-cors')
app.use(cors())//注意這個配置要在router前使用不然不生效解決完跨域后,選擇圖片且上傳.咦~404好吧沒有新增對應的router,走起:
router.post('/upload', async (ctx) => {
ctx.body = 'ok'
})此時咱們已經拿到傳過來的數(shù)據(jù)啦,重頭戲來了:@koa/multer使用
const multer = require('@koa/multer')
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, './upload')
},
filename: function (req, file, cb) {
const fileFormat = (file.originalname).split('.')
cb(null, Date.now() + '.' + fileFormat[fileFormat.length - 1])
}
})
const upload = multer({ storage })配置好后修改/upload
router.post('/upload', upload.single('file'), async (ctx) => {
console.log('ctx.request.body', ctx.request.body)
ctx.body = 'done'
})note:需要注意的是upload.single('file'),中的file需要和上方的index.html中的formData字段一致 此時就可以愉快的上傳啦~~~
到此這篇關于利用node+koa+axios實現(xiàn)圖片上傳和回顯功能的文章就介紹到這了,更多相關node koa axios圖片上傳回顯內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
nodejs命令行參數(shù)處理模塊commander使用實例
這篇文章主要介紹了nodejs命令行參數(shù)處理模塊commander使用實例,commander是一個非常高大上的令行參數(shù)處理模塊,需要的朋友可以參考下2014-09-09
Node.JS 循環(huán)遞歸復制文件夾目錄及其子文件夾下的所有文件
在Node.js中,要實現(xiàn)目錄文件夾的循環(huán)遞歸復制也非常簡單,使用fs模塊即可,僅需幾行,而且性能也不錯,我們先來實現(xiàn)文件的復制,需要的朋友可以參考下2017-09-09
npm?install報錯unable?to?resolve?dependency?tree的解決辦法
在開發(fā)過程中經常會使用npm安裝依賴包來加速開發(fā),但是在執(zhí)行npm install命令時,有時會遇到各種錯誤,下面這篇文章主要給大家介紹了關于npm?install報錯unable?to?resolve?dependency?tree的解決辦法,需要的朋友可以參考下2023-05-05

