[英]Sending data to nodejs server
I'm learning nodejs at the moment and I'm stuck with a few things. 目前,我正在学习nodejs,并且还停留在一些事情上。 I'm creating a server that's serving a html file.
我正在创建一个提供html文件的服务器。 That html file has a js that does an xmlHttpRequest to get data.
该html文件具有一个执行xmlHttpRequest来获取数据的js。 The data I'm retrieving I want to send back to my server to process it.
我要检索的数据要发送回服务器进行处理。 And that last step is where I'm stuck.
最后一步是我遇到的困难。 every time the server stops, while I would like to receive the urls in the server to process them.
每次服务器停止时,我都希望接收服务器中的URL进行处理。
Server.js Server.js
var http = require('http'),
url = require('url'),
path = require('path'),
fs = require('fs');
var mimeTypes = {
"html": "text/html",
"js": "text/javascript",
"css": "text/css"};
http.createServer(function(request, response){
var uri = url.parse(request.url).pathname;
var filename = path.join(process.cwd(), uri);
fs.exists(filename, function(exists){
if(!exists){
console.log(filename + " does not exist");
response.writeHead(200, {'Content-Type' : 'text/plain'});
response.write('404 Not found\n');
response.end();
return;
}
var mimeType = mimeTypes[path.extname().split(".")[1]];
response.writeHead(200, {'Content-Type' : mimeType});
var fileStream = fs.createReadStream(filename);
fileStream.pipe(response);
});
response.on('end', function(){
console.log("Request: " + request);
console.log("Response: " + response);
});
}).listen(1337);
Client.js Client.js
function getURLs(){
var moduleURL = document.getElementById("url").value;
var urls = [];
console.log(moduleURL);
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
var xml = xmlhttp.responseXML;
var items = xml.children[0].children[0].children;
for(var i = 13; i<items.length; i++){
urls.push(items[i].children[1].getAttribute("url")+"&hd=yes");
}
//console.log(urls);
sendDataToServer(urls);
}
}
xmlhttp.open("GET", moduleURL, true);
xmlhttp.send();
}
function sendDataToServer(urls){
//console.log(urls);
var http = new XMLHttpRequest();
http.open("POST", "http://127.0.0.1:1337/", true);
http.send(urls);
}
I'm getting this in the console of the browser 我在浏览器的控制台中得到这个
POST http://127.0.0.1:1337/ net::ERR_CONNECTION_REFUSED
POST http://127.0.0.1:1337/ net :: ERR_CONNECTION_REFUSED
And this in cmd of node 而这在节点的cmd中
events.js:72 throw er;
events.js:72 throw er; // Unhandled 'error' event ^ Error: EISDIR, read
//未处理的“错误”事件^错误:EISDIR,已读取
And while it's processing the data I would like to send back the progress to the client to show it on the html page for the end user. 在处理数据时,我想将进度发送回客户端,以在最终用户的html页面上显示。 I already have the function of the progress it's only the sending/receiving of data where I am stuck.
我已经具有进度功能,只是卡住了我的数据的发送/接收。 Can someone point me in the correct direction please?
有人可以指出我正确的方向吗?
I also know that there is express and other modules that I can use, but to learn node I'm trying it to do it this way. 我也知道可以使用express和其他模块,但是要学习节点,我正在尝试以这种方式进行。 So I hope someone can push me in the correct direction.
因此,我希望有人可以将我推向正确的方向。
events.js:72 throw er;
events.js:72 throw er; // Unhandled 'error' event ^ Error: EISDIR, read
//未处理的“错误”事件^错误:EISDIR,已读取
This error means that the file you're trying to read is actually a directory. 此错误意味着您尝试读取的文件实际上是一个目录。
What you need to do is make sure that the file is really a file as the function fs.exists() is only for files. 您需要做的是确保文件确实是文件,因为函数fs.exists()仅适用于文件。
In this example fs.lstat() is used to get a fs.stat object which has the methods you need to make sure that a file is of the right type. 在此示例中,使用fs.lstat()获取fs.stat对象,该对象具有您需要确保文件类型正确的方法。
var http = require('http'),
url = require('url'),
path = require('path'),
fs = require('fs');
var mimeTypes = {
"html": "text/html",
"js": "text/javascript",
"css": "text/css"
};
http.createServer(function(request, response){
var uri = url.parse(request.url).pathname;
var filename = path.resolve(path.join(process.cwd(), uri));
console.log(filename);
// Get some information about the file
fs.lstat(filename, function(err, stats) {
// Handle errors
if(err) {
response.writeHead(500, {'Content-Type' : 'text/plain'});
response.write('Error while trying to get information about file\n');
response.end();
return false;
}
// Check if the file is a file.
if (stats.isFile()) {
fs.exists(filename, function(exists){
if(!exists){
console.log(filename + " does not exist");
response.writeHead(200, {'Content-Type' : 'text/plain'});
response.write('404 Not found\n');
response.end();
return;
}
var mimeType = mimeTypes[path.extname().split(".")[1]];
response.writeHead(200, {'Content-Type' : mimeType});
var fileStream = fs.createReadStream(filename);
fileStream.pipe(response);
});
} else {
// Tell the user what is going on.
response.writeHead(404, {'Content-Type' : 'text/plain'});
response.write('Request url doesn\'t correspond to a file. \n');
response.end();
}
});
response.on('end', function(){
console.log("Request: " + request);
console.log("Response: " + response);
});
}).listen(1337);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.