HTTP模块加载错误Node.js

时间:2015-10-29 13:32:56

标签: javascript node.js

我是Node.js的新手,并在Windows 7上安装了Node.js(版本4.2.1)。 如果我写:

var http = require("http");

它说undefined,为什么会触发此错误?

2 个答案:

答案 0 :(得分:0)

var http = require("http");
http.createServer(function (request, response) {

    // Send the HTTP header 
    // HTTP Status: 200 : OK
    // Content Type: text/plain
    response.writeHead(200, {'Content-Type': 'text/plain'});

    // Send the response body as "Hello World"
    response.end('Hello World\n');
}).listen(8081);

// Console will print the message
console.log('Server running at http://192.168.168.259:8081/');

答案 1 :(得分:-2)

您需要安装额外的模块,如http,在项目的根文件夹中运行此命令:

npm install http