0.18.0 • Published 8 years ago

roi v0.18.0

Weekly downloads
355
License
Apache-2.0
Repository
github
Last release
8 years ago

roi

Coverage Status Build Status Known Vulnerabilities dependencies Status

NPM

A dependency-free http module.

Installation

npm install roi -S

Usage

const roi = require('roi');

roi.get('http://localhost:3000/posts')
.then(response => {
  console.log(response);
  console.log(response.statusCode);
  console.log(response.headers);
  console.log(response.body);
})
.catch(e => console.log(e));

More examples

POST
const foo = {
  title: 'foo-json',
  author: 'bgold'
};

roi.post('http://localhost:3000/posts', foo)
.then(response => console.log(response)
.catch(e => console.log(e));
PUT
const foo = {
  title: 'foo-json2',
  author: 'bgold'
};

roi.put('http://localhost:3000/posts/2', foo)
.then(response => console.log(response))
.catch(e => console.log(e));
DELETE
roi.del('http://localhost:3000/posts/3')
.then(response => console.log(response))
.catch(e => console.log(e));
HEAD
roi.head('http://localhost:3000/posts/3')
.then(response => console.log(response.statusCode === 200))
.catch(e => console.log(e));
DOWNLOAD
roi.download('https://github.com/bucharest-gold/roi/raw/master/test/green.png', '/tmp/green.png')
.then(x => console.log(x))
.catch(e => console.log(e));
UPLOAD
// Fake server side app will save the file called myFileUploaded.png :
const up = (request, response) => {
  request
    .pipe(fs.createWriteStream('/tmp/myFileUploaded.png'))
    .on('finish', () => {
      response.end(request.headers.filename);
    });
};
const server = require('http').createServer(up);
server.listen(3002, () => {});
// Upload and check if the uploaded file exists:
roi.upload('http://localhost:3002/', '/tmp/myFile.png')
.then(response => {
  console.log(fs.existsSync('/tmp/myFileUploaded.png'));
});
Basic authentication
// Add the username and password:
const options = {
  endpoint: 'http://localhost:3000/',
  username: 'admin',
  password: 'admin'
};
roi.get(options)
.then(response => console.log(response))
.catch(e => console.log(e));
Remove 'Authorization' header

To avoid error like:

Request header field Authorization is not allowed by Access-Control-Allow-Headers.

const options = {
  endpoint: 'http://localhost:3000/',
  noAuth: true
};
roi.get(options)
.then(response => console.log(response))
.catch(e => console.log(e));

Contributing

Please read the contributing guide

0.18.0

8 years ago

0.17.0

8 years ago

0.16.1

8 years ago

0.16.0

8 years ago

0.15.0

9 years ago

0.14.1

9 years ago

0.14.0

9 years ago

0.13.0

9 years ago

0.12.1

9 years ago

0.12.0

9 years ago

0.11.0

9 years ago

0.10.0

9 years ago

0.9.0

10 years ago

0.8.0

10 years ago

0.7.0

10 years ago

0.6.0

10 years ago

0.5.0

10 years ago

0.4.0

10 years ago

0.3.2

10 years ago

0.3.1

10 years ago

0.3.0

10 years ago

0.2.0

10 years ago

0.1.0

10 years ago