我是一名 Node.js 初学者。假设我在 Windows 7 上的 C:\Program Files\nodejs\nodejs.exe
中安装了 apache 服务器(XAAMP)和 node.js。
如何在我的 apache 服务器中运行 node.js 来模拟我的代码?
我的意思是,我知道如何编写 Node.js 代码,但我不知道它在我的服务器上是如何工作的?
最佳答案
Apache 服务器不需要 Node.js。
创建您自己的 Node.js 服务器:
Download并安装 Node.js
创建文件
hello.js
:var http = require("http"); var server = http.createServer().listen(3000); // beter way for create server.on("request", function(req, res){ res.writeHead(200, {"Content-Type": "text/plain"}); // for view at page http://localhost:3000 res.write("Hello world"); res.end(); }); server.on("listening", function(){ // for view in console console.log("Listen: 3000..."); });
在终端中转到文件
hello.js
的目录并输入:node hello.js
打开浏览器并将其指向 http://localhost:3000/ 。这应该显示一个网页,内容如下:
Hello world
关于javascript - 如何在安装了apache服务器的Windows上运行node.js?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31615149/