本文整理汇总了TypeScript中http-proxy.createProxyServer函数的典型用法代码示例。如果您正苦于以下问题:TypeScript createProxyServer函数的具体用法?TypeScript createProxyServer怎么用?TypeScript createProxyServer使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了createProxyServer函数的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1:
const proxy = http.createServer((request, response) => {
let hostHeader = request.headers['host'];
log.debug(
'request made to proxy: ' + request.url + ', ' +
'target: ' + hostHeader);
if (hostHeader.startsWith('http://') || hostHeader.startsWith('127.0.0.1')) {
if (!hostHeader.startsWith('http://')) {
hostHeader = 'http://' + hostHeader;
}
httpProxy.createProxyServer({target: hostHeader}).web(request, response);
} else {
if (!hostHeader.startsWith('https://')) {
hostHeader = 'https://' + hostHeader;
}
httpProxy.createProxyServer({target: hostHeader}).web(request, response);
}
});
示例2: startProxy
startProxy(proxyConfig: any) {
log(`Building proxy to ${config.backendDest}`)
this.server = httpProxy.createProxyServer(proxyConfig)
this.server.on('error', log.warn)
// See https://github.com/nodejitsu/node-http-proxy/issues/180#issuecomment-310550385
this.server.on('proxyReq', (proxyReq, req) => {
if (req.body == null) { return }
const bodyData = JSON.stringify(req.body)
proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData))
proxyReq.write(bodyData)
})
}
示例3: constructor
constructor(config: Config) {
super();
// var middleware = this.intercept.bind(this);
this.config = config;
const { port, protocol, target, targetPort, path } = config;
const url = `${protocol}://${target}:${targetPort || "80"}${path || ""}`;
this.app = connect();
// PROXY SET
config.target = url;
this.proxy = httpProxy.createProxyServer(config);
this.proxy.on('error', (err)=> {
return console.log(err);
});
}
示例4: handle_upgrade
async function handle_upgrade(req, socket, head): Promise<void> {
if (hub_proxy.version_check(req, undefined, base_url)) {
logger.debug("http_proxy: websocket upgrade -- version check failed");
return;
}
let proxy;
const req_url = req.url.slice(base_url.length);
logger.debug(`websocket_proxy.handle_upgrade: "${req_url}"`);
const { type, key, port_number, project_id } = target_parse_req(req_url);
proxy = websocket_proxy_cache[key];
if (proxy !== undefined) {
// easy case -- already have a proxy in the cache.
proxy.ws(req, socket, head);
return;
}
logger.debug("websocket", "upgrade -- creating proxy");
let port;
try {
port = await get_port(
port_number,
type,
project_id,
compute_server,
database
);
} catch (err) {
// TODO: I don't know how to fail this...
//res.status(500).send(`internal error: ${err}`);
return;
}
proxy = createProxyServer({
ws: true,
target: `ws://localhost:${port}`
});
proxy.on("error", function(e) {
logger.debug(
"websocket",
`websocket proxy error, so clearing cache -- ${e}`
);
delete websocket_proxy_cache[key];
});
websocket_proxy_cache[key] = proxy;
proxy.ws(req, socket, head);
}
示例5: ApiProxyMiddleware
function ApiProxyMiddleware(req: express.Request, res: express.Response) {
let proxy = httpProxy.createProxyServer();
let options: httpProxy.ServerOptions = {
target: 'http://127.0.0.1:8081/services'
,changeOrigin: true // change the 'host' header field to target host
};
proxy.web(req, res, options);
proxy.on('error', (err:any, req: express.Request, res:express.Response) => {
console.log('proxy error: ' + JSON.stringify(err));
res.status(500).jsonp({'error': 'internal server error'});
});
proxy.on('proxyReq', (proxyReq:http.ClientRequest, req: express.Request, res: express.Response, options: httpProxy.ServerOptions) => {
//console.log('proxyReq()');
//proxyReq.setHeader('authorization', 'Bearer ' + bearerToken);
});
proxy.on('proxyRes', (proxyRes:http.IncomingMessage, req: express.Request, res: express.Response) => {
//console.log('proxyRes()');
});
}
示例6: require
/// <reference path="node/node.d.ts" />
/// <reference path="express/express.d.ts" />
import os = require("os");
import express = require("express");
var stylus = require("stylus"),
nib = require("nib");
var app = express();
var httpProxy = require("http-proxy");
var hgToZip = require("../HgToZip/server.js");
//beginregion Ugly setup stuff
app.enable("strict routing");
var proxy = httpProxy.createProxyServer();
app.use(express.vhost("elopakao.myfirefly.me", function (req, res) {
console.log("Redirecting to elopakao: " + req.url);
proxy.web(req, res, {
"target": "http://myfirefly.me:8442"
});
}));
app.all('/hgtozip', function (req, res) { res.redirect('/hgtozip/'); });
app.use("/hgtozip/", hgToZip);
var compile = function (str, path) {
return stylus(str)
.set('filename', path)
.use(nib());
};
app.set('views', __dirname + '/views');
示例7: require
import {Router} from 'express';
import * as httpProxy from 'http-proxy';
import * as request from 'request-promise';
import {channelController} from '../controllers/channel.controller';
const config = require('../../../tradejs.config');
const router = Router();
const proxy = httpProxy.createProxyServer({});
proxy.on('error', function (err, req, res) {
console.error(err);
});
/**
* Single
*/
router.get('/:id', async (req, res, next) => {
try {
res.send(await channelController.findById(req.user, req.params.id));
} catch (error) {
console.error(error);
next(error);
}
});
/**
* List
*/
router.get('/', async (req, res, next) => {
try {
res.send(await channelController.findByUserId(req.user, req.query.user, req.query));
示例8: require
///<reference path="typings/cookie-parser/cookie-parser.d.ts"/>
import * as express from 'express';
import * as bodyParser from 'body-parser';
import * as session from 'express-session';
import * as cookie from 'cookie-parser';
declare var GLOBAL:any;
declare var ROOT:string;
declare var WWW:string;
declare var SERVER:string;
var fs = require('fs');
var request = require('request');
var httpProxy = require('http-proxy');
var proxy = httpProxy.createProxyServer({
changeOrigin:true,
port:80
});
var path = require('path');
GLOBAL.ROOT = __dirname;
GLOBAL.WWW = path.resolve(ROOT + '/client/');
GLOBAL.SERVER = path.resolve(ROOT + '/server/');
GLOBAL.onError = function (err: any, res:express.Response) {
console.log('onError error\n', err);
//TODO Remove reason in production
res.json({error:'error', reason:err});
var str: string = "\r\n" + new Date().toLocaleString() + "\r\n";
str += JSON.stringify(err);
示例9: function
serverValue = serverValue ? serverValue : URLHelper.combineURLParts(GlobalConfig.proxy.baseURL, GlobalConfig.proxy.nameSpace);
program.parse(process.argv);
if (typeof serverValue === 'undefined') {
console.error('No server specified!');
program.outputHelp();
process.exit(1);
}
portValue = program.port || 5050;
insecureValue = program.insecure || false;
var proxy = httpProxy.createProxyServer({
secure: !insecureValue
});
proxy.on('proxyRes', function(proxyRes, req, res, options) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,PATCH,OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization, X-Requested-With, X-HTTP-Method-Override, Accept, rest-dspace-token');
res.setHeader('Access-Control-Allow-Credentials', 'true');
});
var server = http.createServer(function (req, res) {
proxy.web(req, res, {target: serverValue}, function(e) {
console.log("Proxy server didn't respond, retrying..");
示例10: App
export function App(config) {
let app = Express();
let proxy = ProxyServer({
agent: new Agent({
socksHost: config.socksHost,
socksPort: config.socksPort
}),
target: config.mpdService,
ignorePath: true
});
proxy.on('error', (err, req, res) => {
res.writeHead(500, {
'Content-Type': 'application/json'
});
res.end(JSON.stringify({
ok: false,
status: 500,
message: '(ノ◕ヮ◕)ノ*:・゚✧ ✧゚・: *ヽ(◕ヮ◕ヽ)'
}));
});
app.all('/listen', (req, res) => {
proxy.web(req, res);
});
return app;
}