I noticed that in some React universal (isomorphic) projects people used the same code to make http requests from client and server using such libraries like isomorphic-fetch or axios. Is that right to make such requests from the server to itself? Can someone explain how such a request happened? Are there any issues or pitfalls?
Pro:
Contra:
Would I do it? No. The overhead is very big and slow. It might be fast enough while you only have few visitors, but that solution will not scale, as it makes several requests out of a single one. In my opinion it is better to use an internal API. You can wire that API to the outside for client-requests (don't forget to add input sanitation), but also use it from inside the server directly.
SHPS (v4.4) example:
// Plugin.js 'use strict'; module.exports.foo = function() { // do something! // This function can be called from any other plugin from code! } module.exports.onRebuildSandbox = function(requestState, sb) { // Wire this plugin into the sandbox, which is used for client-requests sb.myPlugin = { foo: module.exports.foo, }; };// Request Script (in sandbox) 'use strict'; // When a client calls this script from HTTP, the following code will be executed myPlugin.foo();// OtherPlugin.js 'use strict'; const libs = require('node-mod-load').libs; module.exports.handleSomething = function(requestState) { // do whatever... // Call API from code, no HTTP request involved.... libs.plugin.callEvent(requestState, 'foo'); };