我必须使用集成测试来测试网站。该网站将所有请求发送给代理:
var express = require("express"),
http = require("http"),
port = (process.env.PORT || 8001),
server = module.exports = express(),
httpProxy = require('http-proxy');
var proxy = httpProxy.createProxyServer();
// SERVER CONFIGURATION
// ====================
server.configure(function() {
server.use(function(req, res, next) {
console.log(req.url);
if (req.url.indexOf('/any/thing') === 0) {
console.log("url: " + req.url + ",\nheaders: " + req.headers + ",\nmethod: " + req.method);
proxy.web(req, res, {target: 'http://127.0.0.1:1337'});
} else {
//console.log("url: " + req.url + ",\nheaders: " + req.headers + ",\nmethod: " + req.method);
next();
}
});
server.use('/anything', express["static"](__dirname + "/../public"));
server.use(express.errorHandler({
dumpExceptions: true,
showStack: true
}));
server.use(express.bodyParser());
server.use(server.router);
});
// SERVER
// ======
// Start Node.js Server
http.createServer(server).listen(port);
此代理应与此服务器通信:
var http = require('http');
http.createServer(function (req, res) {
console.log("url: " + req.url + ",\nheaders: " + req.headers + ",\nmethod: " + req.method);
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end("{'user': 'garrarufa', 'loginToken': 'token'}");
}).listen(1337, "127.0.0.1");
console.log('Server running at http://127.0.0.1:1337/');
使用登录ajax:
$.ajax(this.apipath + "/login/", {
type: "GET",
async: false,
success: function(...){...},
error: function(...){...}
});
当我运行登录请求时,立即调用error
。请求不会出现在代理服务器上。至少代理不会向控制台打印任何内容。这里发生了什么?不应该将ajax请求发送到服务器吗?
答案 0 :(得分:0)
您的代理正在
上运行port = (process.env.PORT || 8001),
您的客户端代码发送到哪个端口?即什么是this.apipath设置为?
$.ajax(this.apipath + "/login/", {
type: "GET",
async: false,
success: function(...){...},
error: function(...){...}
});