gpt4 book ai didi

javascript - Express JS Node JS multer 集成问题

转载 作者:行者123 更新时间:2023-11-30 16:36:53 29 4
gpt4 key购买 nike

https://github.com/cminhho/AngularFileUpload-Express/blob/master/server.js

我正在实现本教程并相应地做了一些事情,但是当我使用更新的 express js 时,它会在 express js 端引发错误。

错误

    C:\nodefiles\new\node_modules\express\lib\application.js:209
throw new TypeError('app.use() requires middleware functions');
^
TypeError: app.use() requires middleware functions
at EventEmitter.use (C:\nodefiles\new\node_modules\express\lib\application.js:209:11)
at Object.<anonymous> (C:\nodefiles\new\server.js:9:5)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:501:10)
at startup (node.js:129:16)
at node.js:814:3

这是抛出错误的代码

    app.use(express.static(__dirname + '/public')); 
app.use(multer({ dest: './uploads/',
rename: function (fieldname, filename) {
return filename+Date.now();
},
onFileUploadStart: function (file) {
console.log(file.originalname + ' is starting ...')
},
onFileUploadComplete: function (file) {
console.log(file.fieldname + ' uploaded to ' + file.path)
done=true;
}
}));

这可能是因为 Express JS 更新版本请告诉我我应该在服务器端更改什么才能使它工作。

您也可以从 github 存储库中获取所有前端代码。

最佳答案

multer 改变了 api。您遵循的教程已过时。您当然可以使用旧版本的 multer,但存在一些问题。在 github multer 页面中有正确的上传文件使用方法:https://github.com/expressjs/multer来自多个页面:

var express = require('express')
var multer = require('multer')
var upload = multer({ dest: 'uploads/' })

var app = express()

app.post('/profile', upload.single('avatar'), function (req, res, next) {
// req.file is the `avatar` file
// req.body will hold the text fields, if there were any
})

app.post('/photos/upload', upload.array('photos', 12), function (req, res, next) {
// req.files is array of `photos` files
// req.body will contain the text fields, if there were any
})

关于javascript - Express JS Node JS multer 集成问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32592428/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com