Express JS Node JS multer集成问题 [英] Express JS Node JS multer intergration issue

查看:137
本文介绍了Express JS Node JS multer集成问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我正在实现本教程,并且已经做了相应的工作,但是当我使用更新的Express js时,它将在Express js方面引发错误.

I am implementing this tutorial and I have done things accordingly but when I am working with updated express js, it throws an errors on the express js side.

错误

    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的更新版本,请告诉我在服务器端应该进行哪些更改才能使此工作正常进行.

This is probably because of Express JS updated version please tell me what should I change in server side to make this work.

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

You can get all the front end code as well from the github repository.

推荐答案

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

multer changed api. Tutorial which you follow is outdated. You can ofcourse use older version of multer, but there was some problems. In github multer page there is correct way of use upload file: https://github.com/expressjs/multer From multer page:

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
})

这篇关于Express JS Node JS multer集成问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆