2016-10-01 80 views
0

我有經驗在ASP.NET MVC編程。我正在學習如何使用Node.js,但我對Node.js中控制器的外觀有點困惑。什麼是Node.js相當於ASP.NET MVC中的控制器?

下面的代碼在Node.js中看起來像什麼?

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

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

回答

2

你會遇到的一個問題是,ASP.NET是一個有很多「出爐」的平臺。節點是一個更加靈活的環境。 Express是一個常見的Web服務器庫。請參閱Express教程以獲取您的問題的答案。

我一般都發現Scotch IO tutorials是相當有幫助的

+0

感謝您的鏈接!很有幫助。 – mfcastro

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的更多信息。

+0

謝謝!這更有意義。 – mfcastro

相關問題