我是Nodejs的新手。我在route
目录中有一个app.js和一个index.js。我有app.use(multer....)
。我还定义了app.post('filter-reports')
,它实际上将文件内容上传到服务器。
我要执行业务逻辑,并在routes/index.js
文件中配置了我打算配置/filter-reports
路由的路由。请帮我理解我哪里错了。我需要使用multer
上传文件,同时运行index.js文件中的业务逻辑。
app.js源代码:
var express = require('express');
var path = require('path');
var favicon = require('static-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var _ = require('underscore');
var cache = require('js-cache');
var multer = require('multer');
var routes = require('./routes/index');
var users = require('./routes/users');
var app = express();
app.all('*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
next();
});
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(favicon());
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', routes);
app.use('/users', users);
var done=false;
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;
}
}));
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
app.post('filter-reports',function(req,res){
console.log('Working on the filtered reports....');
if(done==true){
console.log(req.files);
res.end("File uploaded.");
}
});
/// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
module.exports = app;
var express = require('express');
var request = require("request");
var _ = require('underscore');
var jscache = require('js-cache');
var schedule = require('node-schedule');
var filename;
在index.js文件中
var router = express.Router();
router.post('/filter-reports', function(req, res) {
console.log('Came inside the Node js router.. Now.. its all up to me to format the data....');
// console.log(req.files);
// console.log('Came insode the filter-reports app url >>>>');
// if(done==true){
// console.log(req.files);
// console.log('Files uploaded succesfully ....');
//res.end("File uploaded.");
// }
});
我的包结构如下:
app.js bin node_modules package.json public routes views
我的package.json是
{
"name": "nodetest1",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node ./bin/www"
},
"dependencies": {
"express": "~4.2.0",
"static-favicon": "~1.0.0",
"morgan": "~1.0.0",
"cookie-parser": "~1.0.1",
"body-parser": "~1.0.0",
"debug": "~0.7.4",
"multer": "~0.1.6",
"jade": "~1.3.0"
}
}
提前致谢, 普拉迪普
答案 0 :(得分:6)
当您将文件夹传递给节点require()
时,它会检查端点的package.json
。如果没有定义,它会检查index.js
,最后检查index.node
(C ++扩展格式)。因此index.js
很可能是需要模块的入口点。
您可以在此处查看http://nodejs.org/api/modules.html#modules_folders_as_modules
通常我使用app.js
作为应用程序主入口点。
答案 1 :(得分:2)
我找到了解决方案。我在router.use
文件中使用了app.use
而不是index.js
。