我只是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)
})
这些是错误消息
node upload.js
/Users/dayatura/Documents/node/node_modules/express/lib/application.js:209 抛出新的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)
at 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)
任何人都可以提供帮助:)
答案 0 :(得分:1)
这些是问题:
useXmlUI=true
然后你可以这样使用它。
var multer = require('multer')(({ dest: '/public/'}));
答案 1 :(得分:0)
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
答案 2 :(得分:0)
我刚从上面的链接中研究了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)
})
答案 3 :(得分:0)
这是使用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) */
})