最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What does a Node.js web service look like? - Stack Overflow

programmeradmin2浏览0评论

I am taking a look at Node.js and thinking about using it for building an API. From what I can tell, ExpressJS would be the web framework and is not what I'd be looking for to solve this.

So what would a web service look like? Would it simply be creating a server, talking to mongo and returning results? Also, what does routing look like? (I'd obviously want to 'design' the routes).

I am taking a look at Node.js and thinking about using it for building an API. From what I can tell, ExpressJS would be the web framework and is not what I'd be looking for to solve this.

So what would a web service look like? Would it simply be creating a server, talking to mongo and returning results? Also, what does routing look like? (I'd obviously want to 'design' the routes).

Share Improve this question asked Apr 18, 2012 at 20:36 Matthew CarriereMatthew Carriere 5116 silver badges13 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

If Express would be your web framework, look at the express-resource (Github) middleware for routing an API. You define resources and it'll wire up REST-style routing for you with very little boilerplate.

app.resource('horses', require('./routes/horses'), { format: json })

Given the above, express-resource will hook up all the REST-style routes to actions you supply, returning JSON by default. In routes/horses.js, you export actions for that resource, along the lines of:

exports.index = function index (req, res) {
  // GET http://yourdomain./horses
  res.send( MyHorseModel.getAll() )
}

exports.show = function show (req, res) {
  // GET http://yourdomain./horses/seabiscuit
  res.send( MyHorseModel.get(req.params.horse) )
}

exports.create = function create (req, res) {
  // PUT http://yourdomain./horses
  if (app.user.canWrite) {
    MyHorseModel.put(req.body, function (ok) { res.send(ok) })
  }
}

// ... etc

You can respond with different representations:

exports.show = {
  json: function (req, res) { 
    // GET http://yourdomain/horses/seabiscuit.json
  }
, xml: function (req, res) {
    // GET http://yourdomain/horses/seabiscuit.xml
  }
}

Middlewares like express-resource can make life with Node and Express much easier, take a look through the examples on github to see if it'll do what you need.

Here is a stub that looks up a horse name from a Postgres database and returns the result as JSON. Clients would access would access the API by going to address such as http://yourdomain./api/horse/seabiscuit

app.get('/api/horse/:name', function(req, res){

    pg.connect(conString, function(err, client) {

        var horse = req.params.name;
        var sql = "...";

        client.query(sql, function(err, result) {
            if (err) {
                ...
            }

            for (var i=0; i<result.rows.length; i++) {
                // Customize data as needed
            }
            return res.send(JSON.stringify(result.rows));
        });
    });
});

Node is pretty low level. It's like C in JavaScript's clothing. Since it's parable to C, there's pretty much a lot you can do with Node. Creating web servers is just one of them. You can create live chat servers using sockets, blogs, streaming etc. The possibilities are infinite. You are limited only by your imagination.

Routing is just a task where you take in mands (monly via URL or headers) and do tasks based on those mands passed.

But even I have not yet scathed the surface of node. It's API is huge and getting bigger. Better try using some basic library like Express or Connect first since they pretty much abstract the basic requirement of building the server from code.

发布评论

评论列表(0)

  1. 暂无评论