无法使用 express 和 multer 运行 node.js 服务器以在节点中上传文件
Posted
技术标签:
【中文标题】无法使用 express 和 multer 运行 node.js 服务器以在节点中上传文件【英文标题】:Cannot run node.js server for upload file in node using express and multer 【发布时间】:2017-03-30 04:22:26 【问题描述】:我只是 node.js 的初学者 我会尝试用 xpress 制作一个网站,这样我就可以在那里上传任何东西。 我在我的 Mac 上本地设置服务器。
这是我写的代码
server.js
var express = require('express');
var app = express();
var fs = require("fs");
var bodyParser = require('body-parser');
var multers = require('multer');
app.use(express.static('public'));
app.use(bodyParser.urlencoded( extended: false ));
app.use(multers( dest: '/public/'));
app.get('/index.html', function (req, res)
res.sendFile( __dirname + "/" + "index.html" );
)
app.post('/file_upload', function (req, res)
console.log(req.files.file.name);
console.log(req.files.file.path);
console.log(req.files.file.type);
var file = __dirname + "/" + req.files.file.name;
fs.readFile( req.files.file.path, function (err, data)
fs.writeFile(file, data, function (err)
if( err )
console.log( err );
else
response =
message:'File uploaded successfully',
filename:req.files.file.name
;
console.log( response );
res.end( JSON.stringify( response ) );
);
);
)
var server = app.listen(8081, function ()
var host = server.address().address
var port = server.address().port
console.log("Example app listening at http://%s:%s", host, port)
)
这些是错误信息
节点上传.js
/Users/dayatura/Documents/node/node_modules/express/lib/application.js:209 throw new TypeError('app.use() 需要中间件函数'); ^
TypeError: app.use() 需要中间件函数 在 EventEmitter.use (/Users/dayatura/Documents/node/node_modules/express/lib/application.js:209:11) 在对象。 (/Users/dayatura/Documents/node/upload.js:10:5)
在 Module._compile (module.js:570:32)
在 Object.Module._extensions..js (module.js:579:10)
在 Module.load (module.js:487:32)
在 tryModuleLoad (module.js:446:12)
在 Function.Module._load (module.js:438:3)
在 Module.runMain (module.js:604:10)
运行时 (bootstrap_node.js:394:7)
在启动时 (bootstrap_node.js:149:9)
请大家帮忙:)
【问题讨论】:
multer 的使用不正确。请检查文档一次github.com/expressjs/multer 【参考方案1】:这些是问题:
var multer = require('multer')(( dest: '/public/'));
那你就可以这样用了。
app.post('/upload', multer.single('image'), function(req, res,next)
//Handle image content here.
console.log(req.file); //to access file
);
【讨论】:
【参考方案2】:根据文档,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
)
有关如何使用任何 multer 方法的文档:https://github.com/expressjs/multer#usage
【讨论】:
【参考方案3】:我刚刚研究了上述链接中的multer
示例。
Js
var express = require('express');
var app = express();
var fs = require("fs");
var multers = require('multer');
var upload = multers( dest: '/public/');
app.get('/index.html', function (req, res)
res.sendFile( __dirname + "/" + "index.html" );
)
app.post('/file_upload', upload.array('avatar'), function (req, res)
for (var i = 0, len = req.files.length; i < len; i++)
var mainFile = req.files[i];
var file = __dirname + "/" + mainFile.name;
fs.readFile( mainFile.path, function (err, data)
fs.writeFile(file, data, function (err)
if( err )
console.log( err );
else
response =
message :'File uploaded successfully',
filename: mainFile.name
;
console.log( response );
res.end( JSON.stringify( response ) );
);
);
)
var server = app.listen(8081, function ()
var host = server.address().address
var port = server.address().port
console.log("Example app listening at http://%s:%s", host, port)
)
【讨论】:
谢谢,我也有同样的询问。【参考方案4】:这是使用 multer 上传文件的完整代码:
var multer = require('multer')
var storage = multer.diskStorage(
destination: function (req, file, cb)
cb(null, './public/app/product') /* here you define destination of uploaded files*/
,
filename: function (req, file, cb)
var ext=file.originalname.split(".")
cb(null, Date.now() +'.'+ext[1]) /* here you set extensions of files when uploaded */
)
var upload = multer( storage: storage );
/* upload.any() method upload any files */
app.post('/backend/product/edit/:id',upload.any(),function (req, res)
/* req.files are all files that uploaded to server */
/* you can see them use res.json(req.files) */
)
【讨论】:
以上是关于无法使用 express 和 multer 运行 node.js 服务器以在节点中上传文件的主要内容,如果未能解决你的问题,请参考以下文章
Express、Multer 和 Cloudinary 未返回完整的 Cloudinary 响应
上传图片,然后使用 Multer 和 Express.js 进行更新