Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does a Node.js web service look like?

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).

like image 900
Matthew Carriere Avatar asked Apr 18 '12 20:04

Matthew Carriere


People also ask

What is Web services in node JS?

It is an open-source, cross-platform runtime environment for developing server-side web applications. Its applications are written using JavaScript and can be run within the Node. js runtime environment on most of the platforms.

Is Netflix made with node js?

Netflix initially used Node. js to enable high volume web streaming to over 182 million subscribers. Their three goals with this early infrastructure was to provide observability (metrics), debuggability (diagnostic tools) and availability (service registration). The result was the NodeQuark infrastructure.

Does node js require a web server?

Strictly speaking, you don't need to put a web server on top of Node. js - you can write a small server within your Node project and have that handle all routine browser requests as well as those particular to the web app concerned. But things like webpage changes are handled better by a web server, e.g. Nginx.


2 Answers

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.com/horses
  res.send( MyHorseModel.getAll() )
}

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

exports.create = function create (req, res) {
  // PUT http://yourdomain.com/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.

like image 179
Luke Vivier Avatar answered Sep 22 '22 11:09

Luke Vivier


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.com/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));
        });
    });
});
like image 40
deltanovember Avatar answered Sep 22 '22 11:09

deltanovember