【问题标题】:How to create and run a file in node.js如何在 node.js 中创建和运行文件
【发布时间】:2013-11-10 14:12:00
【问题描述】:

我安装了 node.js 新版本,并使用 node-v 和 npm-v 签入了命令提示符 我想知道如何创建和运行该文件。

【问题讨论】:

标签: javascript node.js file createjs


【解决方案1】:

在你的项目根目录下创建文件hello.js,并填写如下代码:

var http = require("http");

http.createServer(function(request, response) {
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.write("Hello World");
  response.end();
}).listen(8888);

首先,在命令提示符下使用 Node.js 执行您的脚本:

node hello.js

打开您的浏览器并将其指向 http://localhost:8888/ 。您将看到一条消息“Hello World”。

【讨论】:

    【解决方案2】:

    你可以像这样启动 Node.js 进程:

    % node example.js
    Server running at http://127.0.0.1:1337/
    

    【讨论】:

    • C:\Neetesh\Node.js\node_modules\npm\test\hello.js:1 (function (exports, require, module, __filename, __dirname) { consol.log('Hell ^ ReferenceError : consol 未在 Object. (C:\Neetesh\Node.js\node_modules\npm\test\hello.js:1:6 3) 在 Module._compile (module.js:456:26) 处定义Object.Module._extensions..js (module.js:474:10) 在 Module.load (module.js:356:32) 在 Function.Module._load (module.js:312:12) 在 Function.Module。 runMain (module.js:497:10) 在启动时 (node.js:119:16) 在 node.js:901:3
    • C:\Neetesh\Node.js\node_modules\npm\test\hello.js:1 (function (exports, require, module, __filename, __dirname) { consol.log('Hell ReferenceError: consol 未定义在 Object. (C:\Neetesh\Node.js\node_modules\npm\test\hello.js:1:6 3) 在 Module._compile (module.js:456:26) 在 Object .Module._extensions..js (module.js:474:10) 在 Module.load (module.js:356:32) 在 Function.Module._load (module.js:312:12) 在 Function.Module.runMain (module.js:497:10) 在 node.js:901:3 我在运行文件时收到此错误
    • 我建议查找并修复拼写错误的console.log。这应该会立即修复该错误消息。
    猜你喜欢
    • 1970-01-01
    • 2011-11-09
    • 2018-06-23
    • 2015-11-17
    • 2017-04-01
    • 2020-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多