正在将数据发送到nodejs服务器
Sending data to nodejs server
我现在正在学习nodejs,我遇到了一些问题。我正在创建一个提供html文件的服务器。该html文件有一个js,它执行xmlHttpRequest来获取数据。我正在检索的数据我想发送回我的服务器进行处理。最后一步是我陷入困境的地方。每次服务器停止时,我都希望接收服务器中的url来处理它们。
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
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);
}
我在浏览器的控制台上得到了这个
岗位http://127.0.0.1:1337/net::ERR_CONNECTION_REFUSED
在节点的cmd中
events.js:72投掷者;//未处理的"错误"事件^错误:EISDIR,读取
在处理数据的同时,我想将进度发送回客户端,以便在html页面上显示给最终用户。我已经有了进度的功能——这只是我被卡住的数据的发送/接收。有人能给我指正确的方向吗?
我也知道我可以使用express和其他模块,但为了学习node,我正在尝试这样做。所以我希望有人能把我推向正确的方向。
events.js:72投掷;//未处理的"错误"事件^错误:EISDIR,读取
这个错误意味着你试图读取的文件实际上是一个目录。
您需要做的是确保该文件确实是一个文件,因为函数fs.exists()仅适用于文件。
在本例中,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);
相关文章:
- 与域在同一台计算机上运行的NODEJS服务器的CORS错误
- Nodejs服务器在解析之前生成输入表单
- Nodejs服务器:加载资源失败:服务器的响应状态为404(未找到)
- NodeJS服务器在检索部分时返回505/404错误
- 将数据发布到nodejs服务器到客户端
- 引用nodejs服务器中html中的java脚本文件
- 正在将数据发送到nodejs服务器
- NodeJs:如何在EC2实例中查看PM2运行的NodeJS服务器的日志
- 为什么我可以't在nodeJS服务器上接收文件
- nodeJS服务器性能缓慢且不稳定
- 需要将数据从NodeJS服务器发送回JQuery客户端
- 如何从本地文件夹(磁盘存储)读取或获取图像文件,并使用nodejs服务器将其发送到Angularjs
- 在nodejs服务器上提供文件的更好方法,而不是if/else
- 如何在 NodeJS 服务器中解析 ajax 发送的数组/对象
- 在 nodejs 服务器端运行传单
- 当我尝试在 couchbase nodejs 服务器上运行简单的测试时,我收到“不正确的参数”错误
- Yeoman 应用程序 - 使用自定义 Nodejs 服务器
- JavaScript 在 nodeJS 服务器上导致错误
- Nodejs 服务器端调试
- 将数据从 Java 客户端发送到 NodeJS 服务器