Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/439.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Node.JS-Express:编码风格_Javascript_Node.js_Model View Controller_Coding Style - Fatal编程技术网

Javascript Node.JS-Express:编码风格

Javascript Node.JS-Express:编码风格,javascript,node.js,model-view-controller,coding-style,Javascript,Node.js,Model View Controller,Coding Style,我正在用Node.JS和Express编写一些RESTAPI。因此,对于每个API,我需要做所有常规的工作,比如参数验证、身份验证、授权,然后是实际的业务逻辑。一些sodo代码可以说明这一点: router.get('/users', function (req, res){ async.auto( authenticateCaller(); authorizeCaller(); validateParams(); doG

我正在用Node.JS和Express编写一些RESTAPI。因此,对于每个API,我需要做所有常规的工作,比如参数验证、身份验证、授权,然后是实际的业务逻辑。一些sodo代码可以说明这一点:

router.get('/users', function (req, res){
    async.auto(
        authenticateCaller();
        authorizeCaller();
        validateParams();

        doGetUsers(); 
    )
})
这种风格当然有效,但由于包含了大量额外的前置代码,使得整个功能非常繁琐。我知道在web应用程序编程中,MVC已经被引入到将UI、模块和控制器分离到不同的代码组中,这要干净得多。有没有类似的框架可以帮助实现这一目标?

这就是我所做的

在这里,我使用Routes for Node.js为一个名为Routes的文件夹让路,其中包含所有代码

var routes = require('./routes');
var route_add_user = require('./routes/add_user');
使用此处的路由调用函数。adduser是该js文件中的函数名

 app.get('/adduser', route_add_user.adduser);  

使用中间件。中间件只是一个包含三个参数的函数:

function (req, res, next) {}
在定义任何路由之前,调用router.use()注册中间件。这将导致在执行每个路由之前调用中间件

这些都是中间件的功能:

  • authenticateCaller()
  • 授权调用方()
  • 验证图()

定义一个函数来完成日常工作

 fuction auth(res,req,next){
     authenticateCaller();
    req.isAuthorized = authorizeCaller();
     validateParams();
     next();
    }

 router.get('/users',auth);
 router.get('/users', function (req, res){
   if( req.isAuthorized)
   {..do some stuff here..}
})

这是我在express framework中验证和使用API所遵循的风格之一

register.js
-----------
exports.addUser=函数(req,res){
//做点什么
};
exports.deleteUser=函数(req,res){
//做点什么
};
routes.js
---------
变量寄存器=要求('./寄存器');
get('/register',auth,register.addUser);
get('/deleteUser',auth,register.deleteUser);
//最好将其作为单独的公共文件来重用所有API
函数auth(req,res,next){
//做一些事情来验证你的API

}
谢谢你,马特。这是我缺少的最基本的东西。