0

How to access website content from another server with Express or HTTP

I have a website that holds all data like template website for example

and I have 3 more websites that get access this website template content HTML CSS everything inside website 2 3 and 4 the only defriend is the route like

mysite.com/template1/user1/index.html

mysite.com/template1/user2/index.html

mysite.com/template1/user3/index.html

I want to have inside website **(n)* only code that gets the HTML CSS and js content from the template server the master how I can do that?.

In PHP is something like

$url = $GET(www.masterserve.com/template1/ + user1 ) echo $url

Any example that I can do the same with node.js and express

// Get dependencies

const express = require('express');
const path = require('path');
const http = require('http');
const bodyParser = require('body-parser');

// Get our API routes
const api = require('./server/routes/api');

const app = express();

// Parsers for POST data
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

// Point static path to dist
app.use(express.static(path.join(__dirname, 'dist'))); <-- idont want static 
file only a URL from the master server

// Set our api routes
app.use('/api', api);

// Catch all other routes and return the index file
app.get('*', (req, res) => {
  res.sendFile(path.join(__dirname, 'dist/index.html'));
});

/**
 * Get port from environment and store in Express.
 */
const port = process.env.PORT || '3000';
app.set('port', port);

/**
 * Create HTTP server.
 */
const server = http.createServer(app);

/**
 * Listen on provided port, on all network interfaces.
 */
server.listen(port, () => console.log(`API running on localhost:${port}`));
jfriend00
  • 683,504
  • 96
  • 985
  • 979
George C.
  • 6,574
  • 12
  • 55
  • 80
  • Because you show your server code, I'm confused. Are you trying to fetch HTTP content from another server? In other words, do you want to write code as an HTTP client to get content from an HTTP server? If so, where do you want it to go in your code? – jfriend00 May 27 '17 at 18:03
  • i have 10 websites that have the same code , the only difrends is the user fror example www.mywebsite.com/user1 ,if is the user 1 server the website for the iser1 data – George C. May 27 '17 at 18:09
  • or i have 10 domain names, banana.com , foo.com this domains have the same code the only difrent is the data i want to have 1 domain like master.com that ontein the template and from the webside banana.com to get all the content like iframe src="masterwebsite/user32424" – George C. May 27 '17 at 18:12
  • in this example i get the data from dist/index.html i want to get the data from the master server like www.masterserver/templatewebsite/user1 and i want to get the content from masterserver domain- server inside the www.banana.com – George C. May 27 '17 at 18:14
  • like iframe but I don't want to use an iframe . – George C. May 27 '17 at 18:16

1 Answers1

1

If you're trying to get HTTP content from some other server from within your nodejs app, you can use the request module.

request.get('http://somesite.com/template1/user3/index.html', function(err, response, body) {
    // access data from other web site here
});

If you're trying to stream that data to some other response, you can also .pipe() the data that you requested to another response. The documentation for that module shows lots of examples of how to do that.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • can you sow me how to do that in this example with how to create the server – George C. May 27 '17 at 18:17
  • thnx bro this is what i want – George C. May 27 '17 at 18:18
  • but in this code i show you how to do that in app.js – George C. May 27 '17 at 18:18
  • @George - I've shown you how to get content from another web server. I don't understand what else you want to do with it. Your question offers no details on exactly what else you want so I've helped with all that I understand. Also, questions here on stack overflow should not be like peeling an onion where you keep adding more to your question. You should ask a clear and complete question with your first post, respond immediately when people don't understand and then let people answer. You should not keep adding more questions onto your original question. – jfriend00 May 27 '17 at 18:22
  • @George - If an answer to your original question leaves you with more questions, then after you've tried a bunch of things yourself, you can ask a new question about what else you need help with. – jfriend00 May 27 '17 at 18:24
  • I have modified my code and is not working for some reason , I get the HTML from the website i see it on the console but on the browser is not work – George C. May 27 '17 at 18:55
  • @George - You've done exactly what I asked you not to do. You've changed your question into a different question. Please put your question back how it was and ask a new question. What you show in this particular question does not make a whole lot of sense. You are preloading one piece of content from another site and serving only that content for all http server requests. But, this should be discussed in a different question. – jfriend00 May 27 '17 at 19:01
  • @George - I rolled it back for you to the prior version. If you click on the "edited xxx ago" link immediately below your question, you can see the version history and you can rollback to any prior version. – jfriend00 May 27 '17 at 19:09
  • Thnx . and sory big mistake . https://stackoverflow.com/questions/44220671/were-to-put-the-body-calback-from-the-reguest-get this is the second part of the other question ? :) – George C. May 27 '17 at 19:10