Tag: http

节点js中的请求响应周期如何与外部I / O一起工作

完整的node.js初学者在这里。 我在某处看到了这个“hello world”的例子 // Load the http module to create an http server. var http = require('http'); // Configure our HTTP server to respond with Hello World to all requests. var server = http.createServer(function (request, response) { response.writeHead(200, {"Content-Type": "text/plain"}); response.end("Hello World\n"); }); // Listen on port 8000, IP defaults to 127.0.0.1 server.listen(8000); // Put […]

使用node.js代理HTTP请求的价值

我最近开始开发一个多人游戏,将使用nowjs从服务器状态同步玩家状态。 我是服务器端开发的新手(我所说的很多事情可能被错误地说了),虽然我明白node.js是如何工作的,但我已经看到了通过另一种服务器技术来代理HTTP请求的讨论(一个拉NGINX或阿帕奇)效率。 我不明白为什么这样做会有好处,尽pipe我已经看到了很多关于如何这样做的解释。 我目前的计划是将游戏的网站和信息放在与游戏本身相同的服务器上,所以如果代理节点有任何收益,我很想知道为什么。

Node.js无法加载网页连接超时

我是一个初学者node.js,我安装它在这里: https://github.com/joyent/node/wiki/Installing-Node.js-via-package-manager : https://github.com/joyent/node/wiki/Installing-Node.js-via-package-manager 我试着从控制台,它的工作原理: console.log('Hello World!'); nodejs helloConsole.js > Hello World! 然后我试图在HTTP服务器中完成这个代码: var http = require("http"); http.createServer(function (request, response) { request.on("end", function () { response.writeHead(200, { 'Content-Type': 'text/plain' }); response.end('Hello HTTP!'); }); }).listen(8080); 我从terminal运行它: nodejs hello.js 然后我打开浏览器http://localhost:8080/ ,加载需要很长时间,然后在chrome中给我: Unable to load the webpage because the server sent no data. 和Firefox给了我: The connection has timed […]

node.js不能使用http进行其他的调用

我是新来的node.js,我想做一个HTTP请求。 我跟着一个试图打电话给random.org的教程。 这是我的app.js文件: var http = require('http'); //The url we want is: 'www.random.org/integers/?num=1&min=1&max=10&col=1&base=10&format=plain&rnd=new' var options = { host: 'www.random.org', path: '/integers/?num=1&min=1&max=10&col=1&base=10&format=plain&rnd=new' }; callback = function(response) { var str = ''; //another chunk of data has been recieved, so append it to `str` response.on('data', function (chunk) { str += chunk; }); //the whole response has been […]

开始使用节点,等待本地主机

我是Node.js的新手,所以我想我会检查出来,做一个你好的世界。 我的三台机器都有同样的问题,Win 8,Win 7和Mac。 首先想到这是一个防火墙问题,但我检查,并在Mac和Windows 8的机器(没有检查win7的麻烦)。 当我从terminal运行Node时,浏览器等待localhost,最终超时。 我已经呆了两天了,似乎无法通过Googlefind任何解决scheme。 我错过了什么? 这是我的代码: var http = require("http"); console.log("file loaded"); http.createServer(function (request, response) { request.on("end", function () { response.writeHead(200, { 'Content-Type': 'text/plain' }); response.end('Hello HTTP!'); }); }).listen(8080);

节点http.getparsingJSON正文

我试图从http.getparsing一个JSON体。 我知道JSOn对象是有效的,因为我可以成功地进行评估: var json = document.body.innerText; obj = JSON.parse(json); 但是,这段代码返回一个错误: undefined:1 undefined{ Syntax error, Unexpected token u 我的节点代码: function getCategories(callback){ var body; var urlCats = "http://…"; process.send(urlCats); http.get(urlCats, function(res){ res.on('data', function (chunk) { body += chunk; }); res.on('end', function () { callback(JSON.parse(body)); }); }) } 我已经看到用Node.js调用JSON API,但我不认为它适用,因为我已经正确地获得正文(callback(正文)打印确定,虽然对象开始:undefined {)。 有什么build议么? 我应该只是削减我的身体string的“未定义”部分? 一定有我失踪的东西! 谢谢!

请求服务器不要gzip内容

是否有可能用一个http请求头来要求服务器不要 gzip内容? 我正在使用node.js request.get库进行API调用,看起来内容正在以gzipforms返回。 它只有1 api(我打电话几个)的问题,我想也许他们的服务器configuration错误。 但我想尝试问服务器的非gzip版本。 这是我得到的回应: GET https://www.itbit.com/api/feeds/ticker/XBTUSD 4R &HTpȇ {3y L 3 SJ)$ Qj ) w\d P ('t]{ d@ ? ŔŅ 2 1Y _ -X% uS } Y ` gN? -sP rr6 .셢$ h ] h > <]@ mx- d 鑈 ` +fos r % ~G c E) ̓5pqXK h S < […]

Nodejs http请求不工作

我有一段代码,应该做一个HTTP请求。 程序成功退出没有错误,但我没有看到任何反应,它甚至没有进入callback函数! 起初我以为这是因为HTTP是asynchronous的,并在最后放置一个大循环,但也没有工作。 有谁知道这个问题? 只有第一个控制台日志sendHttpRequest和444被打印。 我也尝试了http.get,但它也没有工作。 function sendHttpRequest (url, callBack) { console.log("sendHttpRequest"); //constrct options var options = { host: 'www.google.com', path: '/index.html', method: 'GET', headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }; http.get("http://www.google.com/index.html", function(res) { console.log("Got response: " + res.statusCode); }); var req = http.request(options, function(res) { console.log("333"); var output = ''; console.log(options.host + ':' + res.statusCode); […]

为什么编辑状态码时不能正确表示redirect?

标准res.redirect('/some/path'); 如预期的那样运行,并立即将请求redirect到/some/path ,但是如果我添加一个状态代码,例如res.redirect(401, '/some/path')并导航到redirect页面,则express不会redirect到/some/path ,而我只是得到以下页面: <p>Unauthorized. Redirecting to <a href="/some/path">/</a></p> 它永远不会redirect。 对于我刚才提供的任何状态码,这都是一样的。 为什么没有指定的代码redirect工作,因为我期待它,我怎样才能返回一个自定义的状态码,并redirect到不同的path?

我的NPM不工作

我的NPM根本不工作。 不仅当我试图安装使用package.json文件,而且当我只是试图安装节点模块。 以下是terminal中出现的内容: npm http GET https://registry.npmjs.org/express/3.3.3 npm http GET https://registry.npmjs.org/express-hbs npm http GET https://registry.npmjs.org/ember npm http GET https://registry.npmjs.org/mongojs npm http GET https://registry.npmjs.org/everyauth npm http GET https://registry.npmjs.org/path npm http GET https://registry.npmjs.org/charlotte npm http GET https://registry.npmjs.org/socket.io npm http GET https://registry.npmjs.org/socket.io-client npm ERR! Error: failed to fetch from registry: mongojs npm ERR! at /usr/share/npm/lib/utils/npm-registry-client/get.js:139:12 npm ERR! at […]