模块在不工作

问题描述:

我有3个文件,一个简单的.html的node.js:模块在不工作

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

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

的 'server.js':

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

和 'index.js':

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

server.proto(); 

全部在同一个文件夹中。我在我的PC上安装了node.js,并在3个文件的文件夹中的Windows cmd“npm install nodejs”上键入。我无法收到来自server.js的警报,我不知道为什么。

首先,你的server.js文件中有语法错误。

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

其次,节点中没有'alert'函数。你可以写CONSOLE.LOG代替

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

然后从命令提示符下运行index.js

node index.js 

你可以看到消息在命令提示符下“工作的功能”。

在浏览器中打开您的html文件不会以相同的方式工作。你真的需要了解节点:)

编辑: 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"); 
}); 

现在,如果您从命令提示符index.js去为localhost:3000在你的浏览器,可以看到它按照你真正想要的方式工作。

哦!alert没有任何东西被node.js识别,它是浏览器的一部分。

而不是使用alert你应该记录你的消息,这也似乎你不是不可复制完整的代码或者这真是一个语法错误,因此要修复它:

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

现在你可以使用您的IDE运行index.js或转到任何shell并拍摄node index,它将运行节点代码。

其次如果你希望你的node.js服务器在浏览器Follow This Tutorial

+0

它为你工作? –

+0

是的,它的确如此。 thxxx –

+0

很好,[node-cheat](https://github.com/zishon89us/node-cheat) –

好发送或打开html文件,安装Node.js的是一个开始,但好像你是无所适从的node.js是。节点是运行时。是的,它建立在V8之上(Google Chrome中的同一个JavaScript VM),但没有DOM,没有浏览器API,也绝对不需要浏览器。因此,alert不存在于Node中。我建议熟悉API docs

正如之前所回答的那样,通过执行node二进制文件并将文件作为参数(有一个repl和其他选项)传递,就像运行python或ruby程序一样运行节点程序。在你的情况下:

node index.js 

首先,Node.js是一个运行时,没有DOM。所以'alert'没有在节点中定义。

其次,你需要通过执行节点的二进制&传递文件名作为命令行参数来运行节点程序,如

node your_file_name.js 

对于在浏览器中获取响应无论是在命令行&你需要做的以下:

命令行:

文件:server.js

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

文件:index.js

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

现在在命令行中运行以下命令:

node index.js 

,你会看到在命令行你所需的输出。

浏览器:

文件:server.js

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

文件: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"); 
}); 

现在在命令行中运行以下命令:

node index.js 

你将在公司中看到以下内容mmand行:

server listening on 3000 

现在就去浏览器&打以下:

http://localhost:3000/ 

您将看到您在浏览器所需的输出。

***欲了解更多信息,我会建议查看'HTTP'节点的API。 https://nodejs.org/api/http.html

谢谢。希望它有帮助...