最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Getting started with node, waiting for localhost - Stack Overflow

programmeradmin1浏览0评论

I am new to Node.js, so I figured I would check it out and do a hello world. I have been having the same issue on all three of my machines, a Win 8, Win 7 and a Mac. Thought at first it was a firewall issue, but I checked and it was off on both Mac and Windows 8 machines (didn't bother checking the win7). When I run Node from the terminal the browser waits for localhost, then eventually times out. I have been at this for two days, can't seem to find any solution via Google. What am I missing.?

Here is my code:

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);

I am new to Node.js, so I figured I would check it out and do a hello world. I have been having the same issue on all three of my machines, a Win 8, Win 7 and a Mac. Thought at first it was a firewall issue, but I checked and it was off on both Mac and Windows 8 machines (didn't bother checking the win7). When I run Node from the terminal the browser waits for localhost, then eventually times out. I have been at this for two days, can't seem to find any solution via Google. What am I missing.?

Here is my code:

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);
Share Improve this question edited Oct 6, 2013 at 23:48 hexacyanide 91.9k31 gold badges166 silver badges162 bronze badges asked Aug 23, 2013 at 2:49 user1210003user1210003 651 silver badge6 bronze badges 1
  • Did you get this code from tuts+? Because I ran into this exact same issue with their node.js tutorial. – SamHuckaby Commented Dec 10, 2013 at 15:35
Add a ment  | 

2 Answers 2

Reset to default 5

You don't need to wait for the HTTP request to end (besides that request.on('end', ..) isn't valid and never fires, and that's why you time out). Just send the response:

var http = require("http");
console.log("file loaded");

http.createServer(function (request, response) {
  response.writeHead(200, {'Content-Type': 'text/plain'});
  response.end('Hello HTTP!');
}).listen(8080);

Although if you want an easier way to create a HTTP server, the simplest way would be to use frameworks such as Express. Then your code would look like this:

var express = require('express');
var app = express();

app.get('/', function (req, res) {
  res.set('Content-Type', 'text/plain');
  res.send(200, 'Hello HTTP!');
});

app.listen(8080);

You can also use the connect middleware. Just install it first using npm like so:

npm install -g connect

After this you can make a very simple app like this:

var app = connect()
  .use(connect.logger('dev'))
  .use(connect.static('public'))
  .use(function(req, res){
    res.end('hello world\n');
  })
 .listen(3000);

You can get more information regarding connect here. I tell you to use this, because you get a very simple server, that is easily extensible. However, if you want to make pull blown web sites, then I would sugges using expressjs.

发布评论

评论列表(0)

  1. 暂无评论