我在尝试接收帖子数据时遇到问题,而没有先从网络浏览器运行html页面。我想知道是否有一种方法使用NodeJS,有人可以运行给定的html文档,静默检索Post数据和控制台输出。我需要这个,以便允许在云或远程服务器环境中进行开发。
当前用法Server.js:
var spawn = require('child_process').spawn; // Will not do for the situation of development within something similar to an Ubuntu Server
spawn(command, ["http://localhost:80/Develop/Client.html"]);
http.createServer(mainArgs).listen(options); // mainArgs is the function (req,res) callback
function mainArgs(req,res){
if (req.method == 'POST') { // NodeJS is Getting what is posted...
console.log("POST");
var url_parts = url.parse(req.url, true);
currentQuery = url_parts.query;
req.on('end', function () {
console.log("Body: " + currentQuery['stubScript']);
writeHTML(currentQuery['stubScript']);
});
}
..... // HTML, JavaScript, & CSS Handled here
}
当前使用情况Client.html:
<html>
<head>
//Posts data via XMLHttpRequest()
<script src=devScript.js></script>
</head>
<body>
<script>
// Access Devscript functions and build page using javascript
</script>
</body>
</html>
当前用法devScript.js:
function postIt(varname, variable) {
var req = new XMLHttpRequest();
var getParms = '?'+ varname + '=' + variable;
req.open('POST', document.URL + getParms, true);
req.onreadystatechange = function(){
if (req.readyState == 4 && req.status == 200)
{
alert('PHPcall() : JSObject =' + varname );
}
};
req.send(getParms);
} // Client is posting...
答案 0 :(得分:1)
您正在寻找发送HTML请求。
您可以使用request等模块或wget或curl等终端程序执行此操作。
但是如果你需要在没有任何模块的情况下这样做(我建议使用模块,它们在许多方面都是理智的,以下情况不是这样)你可以通过使用本机来做this SO thread中讨论的内容http和querystring个模块。
以下是该主题的代码:
// We need this to build our post string
var querystring = require('querystring');
var http = require('http');
var fs = require('fs');
function PostCode(codestring) {
// Build the post string from an object
var post_data = querystring.stringify({
'compilation_level' : 'ADVANCED_OPTIMIZATIONS',
'output_format': 'json',
'output_info': 'compiled_code',
'warning_level' : 'QUIET',
'js_code' : codestring
});
// An object of options to indicate where to post to
var post_options = {
host: 'closure-compiler.appspot.com',
port: '80',
path: '/compile',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(post_data)
}
};
// Set up the request
var post_req = http.request(post_options, function(res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('Response: ' + chunk);
});
});
// post the data
post_req.write(post_data);
post_req.end();
}
// This is an async file read
fs.readFile('LinkedList.js', 'utf-8', function (err, data) {
if (err) {
// If this were just a small part of the application, you would
// want to handle this differently, maybe throwing an exception
// for the caller to handle. Since the file is absolutely essential
// to the program's functionality, we're going to exit with a fatal
// error instead.
console.log("FATAL An error occurred trying to read in the file: " + err);
process.exit(-2);
}
// Make sure there's data before we post it
if(data) {
PostCode(data);
}
else {
console.log("No data to post");
process.exit(-1);
}
});
但是我强烈建议使用request / wget / curl,因为错误处理是集中的,诸如表单(multipart,urlencoded等),json数据和Headers之类的东西在'client'端正确实现,大大减少了数量你会遇到的虫子。
答案 1 :(得分:1)