given a RESTful web service that allows different users to post different articles
For defining the GET
function on the server-sidem
i suppose there should be async functions like list
, getTopViewed
, getTopFavorite
etc functions on the server-side
is the following correct then?
exports.get = function(req, res) {
db.articles.list(function etc)
db.articles.getTopViewed(function etc)
db.articles.getTopFavorite(function etc)
}
NOTE: where list
, getTopViewed
and getTopFavorite
are defined in another JavaScript file
In another JS file:
exports.list = function(callback){
// acts as async callback
var result = ArticleModel.find({}, function(err, articles){
callback(null, articles)
});
return result;
}
I recommend using something like Connect or Express. It has a router that makes this a bit easier. Setup and usage look something like this:
var express = require("express"),
db = require('./yourdb.js');
var app = express.createServer();
app.configure(function(){
app.use(app.router);
});
app.get("/articles/", function(req, res){
db.list(function(err, articles){
res.writeHead(200);
// you probably want to do something more complex here
res.end(JSON.stringify(articles));
});
});
app.get("/articles/top", function(req, res){
// res.end(<top articles go here>);
});
Here's a link to the docs for Express' router: Application Routing