LoginSignup
4
4

More than 5 years have passed since last update.

uupaa.task.jsでリクエストをリトライする

Last updated at Posted at 2014-03-09

uupaa.task.jsを少し使ってみたのです。

package.json
{}
server.js
var http = require('http'),
    server = http.createServer();

var fail = false;

server.on('request', function(req, res) {
  var response;

  fail = !fail;
  response = (fail)
      ? {code: 500, type: 'text/plain', body: 'error'}
      : {code: 200, type: 'application/json', body: '{"status": "success"}'};

  res.writeHead(response.code, {
    'Content-Type': response.type
  });
  res.end(response.body);
});
server.listen(8000);
index.js
var request = require('request'),
    task = require('uupaa.task.js');

var t = new task(1, function(err, buffer) {
  console.log(buffer.status);
}).missable(2);

function req() {
  request('http://localhost:8000/', function(err, res, body) {
    if (res.statusCode === 500) {
      console.error('oh');
      t.miss();
      req();
    } else {
      t.set('status', JSON.parse(body).status).pass();
    }
  });
}
req();
Terminal
$ npm install request uupaa.task.js
Terminal1
$ npm start
Terminal2
$ node index.js
oh
success

なるほどー。いいかも。

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4