Google Closure 编译器移动了?它给出了 302 错误

发布于 2024-11-08 18:17:19 字数 1349 浏览 9 评论 0原文

我正在使用 nodejs 0.4.7 发出请求,这是我的代码:

var post_data = JSON.stringify({  
    'compilation_level' : 'ADVANCED_OPTIMIZATIONS',  
    'output_format': 'json',
      'warning_level' : 'QUIET',
      'js_code' : code
});

var post_options = {  
    host: 'closure-compiler.appspot.com',  
    port: '80',  
    path: 'compile',  
    method: 'POST',  
    headers: {  
        'Content-Type': 'application/x-www-form-urlencoded',  
        'Content-Length': post_data.length  
    }  
}; 

var post_req = http.request(post_options, function(res) {  
    res.setEncoding('utf8');  
    res.on('data', function (chunk) {  
        console.log('Response: ' + chunk);  
    });  
});

post_req.write(post_data);  
post_req.end();

我得到的响应是

Response: <HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="http://www.google.com/">here</A>.
</BODY></HTML>

为什么会发生这种情况?我做错了什么?在教程中,它说我应该向http://closure-compiler.appspot.com/compile...

I'm using nodejs 0.4.7 to make the request, this is my code:

var post_data = JSON.stringify({  
    'compilation_level' : 'ADVANCED_OPTIMIZATIONS',  
    'output_format': 'json',
      'warning_level' : 'QUIET',
      'js_code' : code
});

var post_options = {  
    host: 'closure-compiler.appspot.com',  
    port: '80',  
    path: 'compile',  
    method: 'POST',  
    headers: {  
        'Content-Type': 'application/x-www-form-urlencoded',  
        'Content-Length': post_data.length  
    }  
}; 

var post_req = http.request(post_options, function(res) {  
    res.setEncoding('utf8');  
    res.on('data', function (chunk) {  
        console.log('Response: ' + chunk);  
    });  
});

post_req.write(post_data);  
post_req.end();

And the response I get is

Response: <HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="http://www.google.com/">here</A>.
</BODY></HTML>

Why is this happening ? What am I doing wrong ? In the tutorial it says I'm suposed to make the POST request to http://closure-compiler.appspot.com/compile...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

莫相离 2024-11-15 18:17:19

您正在尝试发送 json 数据:

var post_data = JSON.stringify({  
    'compilation_level' : 'ADVANCED_OPTIMIZATIONS',  
    'output_format': 'json',
      'warning_level' : 'QUIET',
      'js_code' : code
});

Google Closure Compiler API 需要标准表单数据,因此您需要使用querystring。此外,您还需要指定您想要的输出格式(我假设是编译后的代码),如指定的 根据他们的文档

var post_data = querystring.stringify({  
    'compilation_level' : 'ADVANCED_OPTIMIZATIONS',  
    'output_format': 'json',
    'output_info': 'compiled_code',
      'warning_level' : 'QUIET',
      'js_code' : code
});

路径更好地声明如下:

path: '/compile', 

这是完整的概念验证代码:

var http = require('http');
var querystring = require('querystring');

var code ="// ADD YOUR CODE HERE\n" +
"function hello(name) {\n" +
" alert('Hello, ' + name);\n" +
"}\n" +
"hello('New user');\n";

var post_data = querystring.stringify({  
    'compilation_level' : 'ADVANCED_OPTIMIZATIONS',  
    'output_format': 'json',
    'output_info': 'compiled_code',
      'warning_level' : 'QUIET',
      'js_code' : code
});

var post_options = {  
    host: 'closure-compiler.appspot.com',  
    port: '80',  
    path: '/compile',  
    method: 'POST',  
    headers: {  
        'Content-Type': 'application/x-www-form-urlencoded',  
        'Content-Length': post_data.length  
    }  
}; 

var post_req = http.request(post_options, function(res) {  
    res.setEncoding('utf8');  
    res.on('data', function (chunk) {  
        console.log('Response: ' + chunk);  
    });  
});

post_req.write(post_data);  
post_req.end();

使用 node.js 运行它会产生以下结果:

$ node test.js 
Response: {"compiledCode":"alert(\"Hello, New user\");"}

You're trying to send json data:

var post_data = JSON.stringify({  
    'compilation_level' : 'ADVANCED_OPTIMIZATIONS',  
    'output_format': 'json',
      'warning_level' : 'QUIET',
      'js_code' : code
});

Google Closure Compiler API wants standard form data, so you want to use querystring instead. Also you need to indicate the output format you want (compiled code I assume), as specified by their documentation:

var post_data = querystring.stringify({  
    'compilation_level' : 'ADVANCED_OPTIMIZATIONS',  
    'output_format': 'json',
    'output_info': 'compiled_code',
      'warning_level' : 'QUIET',
      'js_code' : code
});

Path is better declared like so:

path: '/compile', 

Here is the full proof of concept code:

var http = require('http');
var querystring = require('querystring');

var code ="// ADD YOUR CODE HERE\n" +
"function hello(name) {\n" +
" alert('Hello, ' + name);\n" +
"}\n" +
"hello('New user');\n";

var post_data = querystring.stringify({  
    'compilation_level' : 'ADVANCED_OPTIMIZATIONS',  
    'output_format': 'json',
    'output_info': 'compiled_code',
      'warning_level' : 'QUIET',
      'js_code' : code
});

var post_options = {  
    host: 'closure-compiler.appspot.com',  
    port: '80',  
    path: '/compile',  
    method: 'POST',  
    headers: {  
        'Content-Type': 'application/x-www-form-urlencoded',  
        'Content-Length': post_data.length  
    }  
}; 

var post_req = http.request(post_options, function(res) {  
    res.setEncoding('utf8');  
    res.on('data', function (chunk) {  
        console.log('Response: ' + chunk);  
    });  
});

post_req.write(post_data);  
post_req.end();

Running it with node.js produces the following:

$ node test.js 
Response: {"compiledCode":"alert(\"Hello, New user\");"}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文