Node.js相当于ASP中的控制器是什么?净MVC



我有ASP编程经验。净MVC。我正在学习如何使用Node.js,但我有点困惑什么控制器会看起来像Node.js。

下面的代码在Node.js中会是什么样子?

[HttpGet]
public Json GetMyResults(){
    //query to database 
}
[HttpPost]
public Json SubmitResults(){
    //query to database 
}

在NodeJS中,控制器的抽象是由你选择使用的框架定义的。

例如,在Express中,你的控制器只是一个带有两个或三个参数的普通函数。

app.get('/users/find', function(req, res) {
  //    
  // The 'req' object contains the request input information
  //
  // This will access the id in query param
  // Ex: /users/find?id=12345
  //
  var userId = req.query.id;
  // Then you'll find it in your database
  Users.findOne({id: userId}).then(function(user) {
     // The 'res' object holds the methods for serving responses
     //
     // Serve a JSON as response with user information
     res.json(user);
  })
});

许多流行的框架都是基于表达式的或受启发的,所以这将是其他项目(如SailsJS)中常见的结构。

您将遇到的一个问题是ASP。. NET是一个具有大量"内置"功能的平台。Node是一个更加灵活的环境。常用的web服务器库是Express。关于您的问题的答案,请参阅Express教程。

我通常发现Scotch IO教程非常有帮助

相关内容

最新更新