【问题标题】:ReferenceError: listen is not defined in NodeJSReferenceError:在 NodeJS 中未定义监听
【发布时间】:2015-02-12 04:17:11
【问题描述】:

我已经在 Ubuntu (14.04) 上安装了 NodeJS,我正在尝试让我的第一个代码工作:

var http = require("http");

http.createServer(function(request,response){
    response.writeHead(200, {"Content-Type": "text/html"});
    response.write("<html>");
    response.write("<head><title>Node.JS</title></head>");
    respons.write("<body>Hello web</body>");
    respons.write("</html>");
    response.end();
}),
listen(9999);

我收到此错误:

/home/privateuser/nodejsweb/helloweb.js:11
listen(9999);
^
ReferenceError: listen is not defined

at Object.<anonymous> (/home/privateuser/nodejsweb/helloweb.js:11:5)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:929:3

在安装 NodeJS 期间,我在安装可选“某物”期间遇到错误: 可选:安装构建工具 要从 npm 编译和安装本机插件,您可能还需要安装构建工具: apt-get install -y build-essential

得到错误:=> E: Kon het vergrendelingsbestand '/var/lib/dpkg/lock' niet openen - open (13: Toegang geweigerd) E: Kan de beheersmap (/var/lib/dpkg/) niet vergrendelen。你对你有什么要求吗?

当我将此命令与 sudo 一起使用时,它告诉我上一个版本已经安装?

然后我尝试了: 自动运行节点 helloweb.js 进入自动适配模式:node helloweb.js 退出命令以退出自动适应模式。

但还是给出了listen的参考错误?

请帮忙,这是我使用 Ubuntu 的第二天,也是我的第一个 NodeJS 示例,我现在已经卡住了:(

提前感谢您的帮助!

【问题讨论】:

    标签: node.js ubuntu


    【解决方案1】:
    var http = require("http");
    
    http.createServer(function(request,response){
        response.writeHead(200, {"Content-Type": "text/html"});
        response.write("<html>");
        response.write("<head><title>Node.JS</title></head>");
        respons.write("<body>Hello web</body>");
        respons.write("</html>");
        response.end();
    }).listen(9999);
    

    listen是一个函数,需要在createServer返回的内容上调用

    【讨论】:

    • 谢谢,把逗号改成点就行了,但后来我收到了响应的参考错误....2typos,将其更改为响应,现在我的第一个示例有效!谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-25
    • 2020-12-26
    • 2013-07-22
    • 2017-04-19
    • 2022-01-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多