/roi

A dependency-free http module client.

Primary LanguageJavaScriptOtherNOASSERTION

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