aboutsummaryrefslogtreecommitdiffstats
path: root/src/nodejs/no2.js
blob: 233b5e17ec52ba1afc80119ec054c70e0e58c937 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
var users = [
  { name: 'tj' },
  { name: 'tim' }
];

function user(app) {
  app.resource('/.:format?', {
    'get' : function(req, res, next) {
      switch (req.params.format) {
        case 'json':
          var body = JSON.stringify(users);
          res.writeHead(200, {
            'Content-Type': 'application/json',
            'Content-Length': body.length
          });
          res.end(body);
          break;
        default:
          var body = '<ul>'
            + users.map(function(user) {
            return '<li>' + user.name + '</li>';
          }).join('\n')
            + '</ul>';
          res.writeHead(200, {
            'Content-Type': 'text/html',
            'Content-Length': body.length
          });
          res.end(body);
      }
    }
  });

  app.resource('/:id.:format', {
    'get' : function(req, res, next) {
      var user = users[req.params.id];
      if (user && req.params.format === 'json') {
        user = JSON.stringify(user);
        res.writeHead(200, {
          'Content-Type': 'application/json',
          'Content-Length': user.length
        });
        res.end(user);
      }
      else {
        // When true is passed, provide control
        // back to middleware, skipping route
        // match attemps
        next(true);
      }
    }
  })

  app.resource('/\\[:id/:op?', {
    'get' : function(req, res) {
      var body = users[req.params.id]
        ? users[req.params.id].name
        : 'User ' + req.params.id + ' does not exist';
      body = (req.params.op || 'view') + 'ing ' + body;
      res.writeHead(200, {
        'Content-Type': 'text/html',
        'Content-Length': body.length
      });
      res.end(body, 'utf8');
    }
  })
}


function main(app) {
  app.resource('/', {
    'get' : function(req, res) {
      var examples = [
        '/users',
        '/users.json',
        '/users/0 (or /users/0/view)',
        '/users/0/edit',
        '/users/0.json'
      ];
      var body = 'Visit one of the following: <ul>'
        + examples.map(function(str) {
        return '<li>' + str + '</li>'
      }).join('\n')
        + '</ul>';
      res.writeHead(200, {
        'Content-Type': 'text/html',
        'Content-Length': body.length
      });
      res.end(body, 'utf8');
    }
  });
}


var connect = require('connect');
var resource = require('resource-router');

var server = connect.createServer(
    connect.logger({ buffer: true }),
    connect.cache(),
    connect.gzip()
  );

server.use('/users', resource(user));
server.use(resource(main));
server.listen(3000);
console.log('Connect server listening on port 3000');