I'm building a gulp task which starts an express server. To get the site to work I need to redirect requests from locahost to a certain IP.
This is what I have right now:
var rewrite = require("connect-url-rewrite");
gulp.task('express', function() {
var express = require('express');
var app = express();
var rules = [
"^localhost\/(.*)$ 192.168.1.1\/$1"
];;
app.use(require('connect-livereload')({
port: 35729
}));
app.get('api/v1', function(req, res, next) {
res.redirect('http://192.168.1.1:8090'+req.originalUrl);
});
app.use(rewrite(rules));
app.use(express.static(__dirname));
app.listen(8090);
});
When I make a request to localhost:8090/api I want a redirect to 192.168.1.1:8090/api.
How can I rewrite the hostname? The only thing I get to work is rewriting the pathname.
Update:
When I add an redirect the request gets canceled. How can I pass all headers to my res object?
One easy way for proxying the AJAX calls to your backend with Gulp would be to use gulp-webserver. Example:
var gulp = require('gulp'),
webserver = require('gulp-webserver');
gulp.task('server', function() {
gulp.src('./public')
.pipe(webserver({
port: 8090,
livereload: true,
proxies: [
{
source: '/api',
target: 'http://192.168.1.1:8090/api'
}
]
}));
});
This will proxy all routes under /api
, ie. call to http://localhost:8090/api/v1/users/1
would proxy the request to http://192.168.1.1:8090/api/v1/users/1
.
./public
in the example is the root folder for the static assets the web server serves, ie. the folder to which you copy the compiled asset files in your Gulpfile.