什么是ASP.NET MVC中的控制器等效的Node.js?

时间:2016-10-01 20:21:10

标签: c# asp.net asp.net-mvc node.js asp.net-mvc-4

我有使用ASP.NET MVC编程的经验。我正在学习如何使用Node.js,但我对Node.js中控制器的外观有点困惑。

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

{{1}}

2 个答案:

答案 0 :(得分:3)

在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中的通用结构。 有关快速结帐的详细信息,请official website

答案 1 :(得分:2)

您将遇到的一个问题是,ASP.NET是一个有很多“烘焙”的平台。 Node是一个更灵活的环境。一个常见的Web服务器库是Express。请参阅Express教程以获取问题的答案。

我一般认为Scotch IO tutorials非常有帮助

相关问题