多个var是节点app.js

时间:2016-02-18 03:23:03

标签: javascript node.js

有多个var调用是不好的形式,例如:

 var init = require('./config/init')(),
     config = require('./config/config'),
     express = require('express'),
     errorHandler = require('errorhandler'),
     bodyParser = require('body-parser'),
     expressValidator = require('express-validator'),
     mongoose = require('mongoose'),
     path = require('path'),
     _ = require('lodash'),
     passport = require('passport'),
     passport_config = require('./config/passport'),
     session = require('express-session'),
     MongoStore = require('connect-mongo')(session),
     swagger = require("swagger-node-express"),
     secrets = require('./config/secrets'),
     multer = require('multer');

或者我应该自己设置每一个?

1 个答案:

答案 0 :(得分:4)

由于您正在使用它在全局范围内导入npm模块,所以很好。但总的来说,为每个变量声明var是一种更好的做法。缺少,的原因是在全局范围内创建以下任何变量。例如,尝试捕获此错误:

var init = require('./config/init')(),
     config = require('./config/config'),
     express = require('express'),
     errorHandler = require('errorhandler'),
     bodyParser = require('body-parser')
     expressValidator = require('express-validator'),
     mongoose = require('mongoose'),
     path = require('path'),
     _ = require('lodash'),
     passport = require('passport'),
     passport_config = require('./config/passport'),
     session = require('express-session'),
     MongoStore = require('connect-mongo')(session),
     swagger = require("swagger-node-express"),
     secrets = require('./config/secrets'),
     multer = require('multer');