A dependency-free http module.
npm install roi -S
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));
const foo = {
title: 'foo-json',
author: 'bgold'
};
roi.post('http://localhost:3000/posts', foo)
.then(response => console.log(response)
.catch(e => console.log(e));
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));
roi.del('http://localhost:3000/posts/3')
.then(response => console.log(response))
.catch(e => console.log(e));
roi.head('http://localhost:3000/posts/3')
.then(response => console.log(response.statusCode === 200))
.catch(e => console.log(e));
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));
// 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'));
});
// 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));
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));
Please read the contributing guide