简体   繁体   English

node.js 中的模块不起作用

[英]Modules in node.js not working

I have 3 files, one simple .html:我有 3 个文件,一个简单的 .html:

 <!DOCTYPE html>
 <html>
 <head>
     <title>meh server</title>
 </head>

 <body>
     <script src = "index.js"></script>
 </body>
 </html>

The 'server.js': 'server.js':

module.exports.proto = function() {
        alert("Working function");
    }
};

And the 'index.js':和'index.js':

 var server = require('./server');

 server.proto();

All in the same folder.都在同一个文件夹中。 I have node.js installed on my PC and typed on the windows cmd ''npm install nodejs'' on the folder of the 3 files.我在我的 PC 上安装了 node.js 并在 Windows cmd ''npm install nodejs'' 上的 3 个文件的文件夹中输入。 I can't receive the alert from server.js and I don't know why.我无法从 server.js 收到警报,我不知道为什么。

First of all, Node.js is a runtime, there is no DOM.首先,Node.js 是一个运行时,没有 DOM。 So 'alert' is not defined in node.所以“警报”没有在节点中定义。

Secondly, you need to run a node program by executing the node binary & passing file name as command line argument, like其次,您需要通过执行节点二进制文件并将文件名作为命令行参数传递来运行节点程序,例如

node your_file_name.js

For getting response both in command line & in browser you need to do the following:要在命令行和浏览器中获得响应,您需要执行以下操作:

Command Line:命令行:

file: server.js文件:server.js

module.exports.proto  =  function () {
    console.log("Working function");
}

file: index.js文件:index.js

var server = require("./server");
server.proto();

now run following command in command line:现在在命令行中运行以下命令:

node index.js

you will see your desired output in command line.您将在命令行中看到所需的输出。

Browser:浏览器:

file: server.js文件:server.js

module.exports.proto  =  function () {
    return "Working function";
}

file: index.js文件:index.js

var server = require("./server");

var httpServer = require("http");
httpServer.createServer(function (request, response) {
    response.writeHead(200, {"Content-Type": "text/html"});
    response.write(server.proto());
    response.end();  
}).
listen(3000, function () {
    console.log("server listening on 3000");
});

now run following command in command line:现在在命令行中运行以下命令:

node index.js

you will see following in the command line:您将在命令行中看到以下内容:

server listening on 3000

now go to browser & hit the following:现在转到浏览器并点击以下内容:

http://localhost:3000/

You will see your desired output in your browser.您将在浏览器中看到所需的输出。

*** For more info I would recommend to look into 'http' API of node. *** 有关更多信息,我建议查看节点的“http”API。 https://nodejs.org/api/http.html https://nodejs.org/api/http.html

Thanks.谢谢。 Hope it helps...希望能帮助到你...

First of all, you have syntax error in your server.js file.首先,您的 server.js 文件中有语法错误。

module.exports.proto = function() {
   alert("Working function");
};

Secondly, you dont have 'alert' function in node.其次,您在节点中没有“警报”功能。 You can write console.log instead您可以改为编写 console.log

 module.exports.proto = function() {
       console.log("Working function");
    };

Then run index.js from command prompt然后从命令提示符运行 index.js

node index.js

You can see the message "Working function" in the command prompt.您可以在命令提示符中看到消息“工作功能”。

Opening your html file in your browser wont work the same way.在浏览器中打开 html 文件不会以相同的方式工作。 You really need to understand node :)你真的需要了解节点:)

EDIT: index.js编辑: index.js

var server = require('./server');

 var http = require('http');
 fs = require('fs');

 http.createServer(function(req, res) {
     server.proto();
     fs.readFile('index.html', 'binary', function(err, file) {
      if(err) {
         res.writeHead(500, {"Content-Type": "text/plain"});
         res.write(err + "\n");
         res.end();
         return;
      }

      res.writeHead(200, {"Content-Type": "text/html"});
      res.write(file, "binary");
      res.end();
      });
 }).listen(3000, function() {
    console.log("Server started");
 });

Now if you run index.js from command prompt go to localhost:3000 in your browser, you can see it working the way you actually wanted.现在,如果您从命令提示符运行 index.js,请在浏览器中转到 localhost:3000,您可以看到它以您真正想要的方式工作。

Oh!哦! alert is not anything recognized by node.js , It is part of browser. alert不是node.js识别的任何东西,它是浏览器的一部分。

Instead of using alert you should be logging your message, It also seems you either have not copied full code or it's really a syntax error , so fix it as:您应该记录您的消息而不是使用alert ,而且您似乎没有复制完整代码或者它确实是一个语法错误,因此将其修复为:

module.exports.proto = function() {
    console.log("Working function");
}

Now you can use your IDE to run index.js or goto any shell and shoot node index , it will run node code.现在您可以使用您的 IDE 运行index.js或转到任何 shell 并拍摄node index ,它将运行节点代码。

Secondly If you want your node.js server to send or open html file in browserFollow This Tutorial其次,如果您希望您的node.js服务器在浏览器中发送或打开html文件,请遵循本教程

Well, installing Node.js is a start, but it seems like you are confused about what node.js is.好吧,安装 Node.js 是一个开始,但您似乎对 node.js 是什么感到困惑。 Node is the runtime.节点是运行时。 Yes it is built on top of V8 (same JavaScript VM in Google Chrome), but there is no DOM, none of the Browser API's, and absolutely no need for a Browser.是的,它建立在 V8 之上(Google Chrome 中的 JavaScript VM),但是没有 DOM,没有浏览器 API,也绝对不需要浏览器。 So, alert doesn't exist in Node.因此,节点中不存在alert I recommend getting familiar with the API docs .我建议熟悉API 文档

As previously answered, you run a node program by executing the node binary and passing a file as an argument (there is a repl, and other options) just like you would run a python, or ruby program.如前所述,您通过执行node二进制文件并将文件作为参数传递(有 repl 和其他选项)来运行 node 程序,就像运行 python 或 ruby​​ 程序一样。 In your case:在你的情况下:

node index.js

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM