0

How to serve static file e.g index.html from different server

app.use(express.static('http://example.com/'))

3
  • 1
    Why would you not just set the source of the file to the external server? Is this an image? CSS? or? Commented Oct 16, 2018 at 14:12
  • Its html page, i want to show data sent by express server to html page that i serve from different location Commented Oct 16, 2018 at 14:15
  • You could use an iFrame to do that. If you need to pass information to the external page, you can use querystring parameters. Commented Oct 16, 2018 at 14:22

1 Answer 1

1

A proxy such as http-proxy-middleware can do this:

var express = require('express');
var proxy = require('http-proxy-middleware');

var app = express();

app.use('/api', proxy({target: 'http://www.example.org', changeOrigin: true}));
app.listen(3000);

// http://localhost:3000/api/foo/bar -> http://www.example.org/api/foo/bar

see https://www.npmjs.com/package/http-proxy-middleware

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.