Suppose the following simple example.
We have Node app.js rendering a React component on the server side:
let http = require('http');
let React = require('react');
let ReactDOMServer = require('react-dom/server');
let Component = require('./Component.js');
http.createServer((req, res) => {
res.writeHead(200);
let markup = ReactDOMServer.renderToStaticMarkup(Component());
res.end(markup);
}).listen(3000);
And our Component.js looks like:
let React = require('react');
function Component() {
return React.createElement('div', {className:'some_class'}, 'Content');
}
module.exports = Component;
A developer working on React components has an access only to the directory with them e.g. /src.
The question is, can he somehow add some code to any of the components to be able to fetch external resource via Node.js?
I mean if he for example would add let http = require('http'); to the component source code JS file and then change the component function like
function Component() {
http.get('http://example.com', (contents) => {
contents.pipe(res);
});
}
he will receive the error
res is not defined
But still, is there a way for him to add something to the component JS file in order to fetch an external resource via Node.js as the server?
And not just using http.get() but any other way including Express.js etc. And also including Next.js