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 

 

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