-2

I have the following express code for get method:

var express = require('express');
var app = express(); 
app.get('/', function(req, res) {
   res.sendFile(path.join(__dirname, '../public/index.html'));
});
app.listen(3000);

I want this code snippet to do:

  1. load a js file from a different server
  2. embed this js file inside the index.html file that was brought by the get request.
Renato Gama
  • 16,431
  • 12
  • 58
  • 92
CrazySynthax
  • 13,662
  • 34
  • 99
  • 183
  • Are they both on the same domain? (Can this even be done server side...?) – evolutionxbox Jan 31 '17 at 10:14
  • Do you want to add a `script` tag to `index.html` ? If so, should it contain the URL to the script on the other server ? Or the content of the script itself ? – A.Perrot Jan 31 '17 at 10:17
  • @evolutionxbox - no. The js file will have to be brought from different domain. – CrazySynthax Jan 31 '17 at 10:18
  • 2
    1. https://www.google.co.uk/webhp?hl=en&sa=X&ved=0ahUKEwiD1t6MlOzRAhXGOhQKHbXiAvAQPAgD#safe=off&hl=en&q=site:stackoverflow.com+node.js+make+http+request 2. http://stackoverflow.com/questions/1787716/is-there-a-template-engine-for-node-js – Quentin Jan 31 '17 at 10:19
  • @A.Perrot - I want to bring the content. The index.html file cannot be changed. I want an external operation that inserts the content of js file into the index.html – CrazySynthax Jan 31 '17 at 10:20

1 Answers1

1

You want to fetch the distant js file with an http request : see the doc.

Then, you want to use a template engine to insert the content of the file into index.html (which has to be ready to be templated), see express doc about template engines.

A.Perrot
  • 323
  • 1
  • 8