我创建了一些代码(一个简单的文件上传器),它在一个文件中运行良好。我想开始将代码分解成几个文件。我想有一个routes.js(控制路由),imports.js(包含我的功能)和我的服务器。我在从路线调用我的导入时遇到问题。我收到错误导入不是一个功能。谢谢!!
Server.js -------------------------------------------- ---------------
var express = require('express');
var app = express();
var passport = require('passport');
var ejs = require('ejs');
var bodyParser = require('body-parser');
var imports = require('./src/import.js');
var port = process.env.PORT || 8080;
app.use(express.static('public'));
require('./src/import')(imports);
require('./app/routes.js')(app, imports);
app.listen(port);
console.log('The app is running on port ' + port)
routes.js -------------------------------------------- ---------------
module.exports = function (app, imports) {
//home Page
app.get('/', function (req, res) {
res.render('index.ejs');
});
app.post('/import', imports(){});
};
import.js -------------------------------------------- -
var multer = require('multer');
var fs = require('fs');
module.exports = function (imports) {
var imports = multer({ dest: 'C:/Users/ron/temp/' });
var type = imports.single('file');
imports( type, function (req, res) {//removed type
var fileType = req.file.type;
console.log('output' + req.file);
console.log(fileType);
var file = __dirname + "/" + req.file.name;
fs.readFile(req.file.path, function (err, data) {
fs.writeFile(file, data, function (err) {
if (err) {
console.log(err);
} else {
var fileName = req.file.originalname;
response = {
message: 'File uploaded successfully',
fileinfo: req.file,
filename: fileName
};
}
console.log(response);
res.end(JSON.stringify(response));
var filePath = req.file.destination + req.file.originalname;
fs.rename(req.file.destination + req.file.filename, req.file.destination + req.file.originalname, function (err) {
if (err) console.log('ERROR:' + err);
console.log('the path is...' + filePath);
console.log('the name is...' + fileName);
});
});
});
});
};
单个文件解决方案............................................ ......
var express = require('express');
var app = express();
var passport = require('passport');
var ejs = require('ejs');
var bodyParser = require('body-parser');
var multer = require('multer');
var fs = require('fs');
var port = process.env.PORT || 8080;
var imports = multer({ dest: 'C:/Users/ron/temp/' });
var type = imports.single('file');
var urlencodedParser = bodyParser.urlencoded({extended: false});
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static('public'));
app.get('/', function (req, res) {
res.render('index.ejs');
});
app.post('/import', type, function (req, res) {//removed type
var fileType = req.file.type;
console.log('output' + req.file);
console.log(fileType);
var file = __dirname + "/" + req.file.name;
fs.readFile(req.file.path, function (err, data) {
fs.writeFile(file, data, function (err) {
if (err) {
console.log(err);
} else {
var fileName = req.file.originalname;
response = {
message: 'File uploaded successfully',
fileinfo: req.file,
filename: fileName
};
}
console.log(response);
res.end(JSON.stringify(response));
var filePath = req.file.destination + req.file.originalname;
fs.rename(req.file.destination + req.file.filename, req.file.destination + req.file.originalname, function (err) {
if (err) console.log('ERROR:' + err);
console.log('the path is...' + filePath);
console.log('the name is...' + fileName);
});
});
});
});
app.listen(port);
console.log('The app is running on port ' + port)
答案 0 :(得分:0)
请注意,路由器中包含的每个功能都必须具有reqest和response对象作为参数。
将您的功能更改为:
function imports (req,res){
//your code
}
module.exports ={
imports:imports
}
现在在路由器文件中导入此功能。