nodejs require 'http'报错

文本server.js中有如下代码:

var http=require('http');
http.createServer(function(request,response){
    response.writeHead(200,{'Content-Type':'text/plain'});
    response.end("HelloWorld");
}).listen(8888);
console.log("server is running");

打开命令窗口,执行 node server.js 抛出SyntaxError : unexpected identifier

因为不确定错误的位置,将console.log移到第二行,仍然报错,怀疑是没有找到http

cmd执行npm install http -g 重新安装http后无错误

访问 http://localhost:8888 返回 HelloWorld 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章