简体   繁体   English

从浏览器运行node.js命令

[英]running node.js command from browser

I've got node.js running on my server and it seems to be working. 我的服务器上运行了node.js,它似乎正在运行。 I'm able to run a .js script from the command line. 我可以从命令行运行.js脚本。

This is one basic script I've been testing: 这是我一直在测试的一个基本脚本:

var http = require('http');

var server = http.createServer(function(req, res) {
  res.writeHead(200);
  res.end('Hello World');
});
server.listen(8080);  

Once I run the script as follows from the command line: 一旦我从命令行运行脚本如下:

node test.js

I am able to go to localhost:8080 and see the response. 我能够转到localhost:8080并查看响应。

So that is great. 太好了。 But I need to be able to do this from the browser. 但是我需要能够通过浏览器执行此操作。 My understanding based on research and examples is that I have to include this file within the web page: 根据研究和示例,我的理解是我必须将此文件包含在网页中:

<script src="socket.io/socket.io.js"></script>

From what I've found in my research, this doesn't refer to an actual file location but if node.js is installed it will get the proper library. 根据我的研究发现,这并不涉及实际的文件位置,但是如果安装了node.js,它将获得适当的库。 But all I get is a 404 file not found error. 但是我得到的只是一个404文件未找到错误。 There are various socket.io.js files on the server that got dropped there during the node.js installation, but none seem to have the required functions for running the script. 服务器上有各种socket.io.js文件,它们是在node.js安装过程中放到那里的,但似乎没有一个文件具有运行脚本所需的功能。

From PHP I can run this .js file from the command line. 从PHP,我可以从命令行运行此.js文件。 Some I'm not concerned that the file can't be run. 一些我不担心该文件无法运行。 But I want to be able to listen for changes on the client side, so I'd still need to have a separate browser-based script to listen for these posts. 但是我希望能够监听客户端的更改,因此我仍然需要有一个单独的基于浏览器的脚本来监听这些帖子。

This doesn't seem like it should be that difficult to determine, but I've been going through the web for the past two days and haven't had any luck. 这似乎似乎很难确定,但是过去两天我一直在网上浏览,没有任何运气。 Your help is appreciated. 感谢您的帮助。

You're going to want to use /socket.io/socket.io.js . 您将要使用/socket.io/socket.io.js That's a url on your server. 那是您服务器上的URL。 If you say just socket.io/socket.io.js , that's a web page on the socket.io domain (see here: socket.io/socket.io.js ) which doesn't exist, so you're getting a 404. 如果仅说socket.io/socket.io.js ,那是不存在的socket.io域上的网页(请参见此处: socket.io/socket.io.js ),因此您将获得一个404。

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

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