2016-10-06 7 views
0

FeatherJS(Expressに基づくNodeJSフレームワーク)アプリケーションからリモートREST-APIを呼び出す方法が必要です。featherJSアプリケーションからリモートレストサービスを呼び出すためのベストプラクティス

私は結構です要求モジュールを使用することを示唆いくつかの記事を見つけた:https://github.com/request/request

は私がFeatherJSを使用している今という任意のより良い提案はありますか?または、リクエストモジュールは問題ありませんか?

+0

私は、モジュールの要求よりも少し便利だと思われるモジュールが見つかりました。 –

答えて

1

フェザーサービスは約束を期待しているので、request-promiseモジュールをお勧めします。ここに例のサービスは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); 
+0

Thx私も同じ結論に達しました。他のより優れた選択肢がない限り、私はこれを答えとしてマークします。 –

関連する問題