Reputation: 2476
I need a way to call remote REST-APIs from my FeatherJS (NodeJS framework based on Express) application.
I have found several posts suggesting to use the request module which is fine: https://github.com/request/request
Are there any better suggestions now that I'm using FeatherJS? Or is the request module just fine?
Upvotes: 1
Views: 1706
Reputation: 44215
I recommend the request-promise
module since Feathers services expect promises. Here is an example service from this post how to make an existing API real-time:
const feathers = require('feathers');
const rest = require('feathers-rest');
const socketio = require('feathers-socketio');
const bodyParser = require('body-parser');
const handler = require('feathers-errors/handler');
const request = require('request-promise');
// A request instance that talks to the API
const makeRequest = request.defaults({
baseUrl: 'https://todo-backend-rails.herokuapp.com',
json: true
});
const todoService = {
find(params) {
return makeRequest(`/`);
},
get(id, params) {
return makeRequest(`/${id}`);
},
create(data, params) {
return makeRequest({
uri: `/`,
method: 'POST',
body: data
});
},
update(id, data, params) {
// PATCH and update work the same here
return this.update(id, data, params);
},
patch(id, data, params) {
return makeRequest({
uri: `/${id}`,
method: 'PATCH',
body: data
});
},
remove(id, params) {
// Retrieve the original Todo first so we can return it
// The API only sends an empty body
return this.get(id, params).then(todo => makeRequest({
method: 'DELETE',
uri: `/${id}`
}).then(() => todo));
}
};
// A normal Feathers application setup
const app = feathers()
.use(bodyParser.json())
.use(bodyParser.urlencoded({ extended: true }))
.configure(rest())
.configure(socketio())
.use('/todos', todoService)
.use('/', feathers.static(__dirname))
.use(handler());
app.listen(3030);
Upvotes: 2