如何在PHP页面中运行Node.js脚本?

时间:2021-11-09 20:59:24

I'm a PHP developer since 2010. I love PHP, just because it's simple. But I want to learn more about Node.js. It looks interesting, specially because I know JavaScript and I'm a big fan of it.

我是自2010年以来的PHP开发人员。我喜欢PHP,因为它很简单。但我想了解更多关于Node.js.它看起来很有趣,特别是因为我了解JavaScript并且我很喜欢它。

Does anyone know how do I run a Node.js script in my HTML page, without displaying the source code, as PHP does? Is that way how it really works?

有没有人知道如何在我的HTML页面中运行Node.js脚本,而不像PHP那样显示源代码?这是真的有效吗?

I've seen many tutorials that they execute Node.js in a terminal, but I didn't find a quick way to run Node.js in a simple HTML page.

我已经看过许多教程,他们在终端中执行Node.js,但我没有找到一种在简单的HTML页面中运行Node.js的快捷方法。

Thank you! :)

谢谢! :)

2 个解决方案

#1


3  

You seem to be conflating two different features of PHP:

您似乎在混淆PHP的两个不同功能:

  1. Many web servers can be configured to run PHP programs through a PHP interpreter and serve the results to the browser.
  2. 许多Web服务器可以配置为通过PHP解释器运行PHP程序,并将结果提供给浏览器。
  3. PHP is designed as a template language with bells on, so PHP code is embedded in a template.
  4. PHP被设计为带有bells的模板语言,因此PHP代码嵌入在模板中。

If you are using Node.js then you will typically:

如果您使用的是Node.js,那么通常会:

  1. Write your webserver in Node.js (although you might configure a front end proxy for it). There is an example of this on the Node.js homepage, but there are also various frameworks, such as express, which do a lot of the heavy lifting for you.
  2. 在Node.js中编写您的Web服务器(尽管您可以为它配置前端代理)。在Node.js主页上有一个这样的例子,但也有各种框架,比如express,它为你做了很多繁重的工作。
  3. Keep your template code separate from your program code. Node has many template modules available for it.
  4. 将模板代码与程序代码分开。 Node有许多可用的模板模块。

Quoted from the Node.js homepage:

引自Node.js主页:

An example: Webserver

This simple web server written in Node responds with "Hello World" for every request.

用Node编写的这个简单的Web服务器响应每个请求的“Hello World”。

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');

To run the server, put the code into a file example.js and execute it with the node program from the command line:

要运行服务器,请将代码放入example.js文件中,并使用命令行中的节点程序执行它:

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

Here is an example of a simple TCP server which listens on port 1337 and echoes whatever you send it:

下面是一个简单的TCP服务器示例,该服务器侦听端口1337并回显您发送的任何内容:

var net = require('net');

var server = net.createServer(function (socket) {
  socket.write('Echo server\r\n');
  socket.pipe(socket);
});

server.listen(1337, '127.0.0.1');

#2


0  

The php code that you have been writing likely is only the html template version of php (for lack of better terms...)

您编写的PHP代码可能只是php的html模板版本(缺少更好的术语......)

When a .php page is requested in the browser, a php interpreter is invoked that parses the php tags in the html and replaces it with html/text. That result is then sent to the browser.

当在浏览器中请求.php页面时,调用php解释器来解析html中的php标记并用html / text替换它。然后将该结果发送到浏览器。

node.js doesn't work that way.

node.js不能那样工作。

Node.js is a lot more.... verbose than php when it comes to this specific topic. node.js is FAR more than just a web application framework or webserver, it can also be used as an executable of sorts to run common tasks.

当涉及到这个特定主题时,Node.js比php更详细。 node.js不仅仅是一个Web应用程序框架或Web服务器,它还可以用作运行常见任务的各种可执行文件。

typically, to get the kind of functionality you are looking for in node.js, you would use a templating framework such as handlebars along with express to handle the webserver and routing. Here's an example:

通常,要获得node.js中要查找的功能,您可以使用模板框架(如handlebars)和express来处理Web服务器和路由。这是一个例子:

// this is just an example, it may or may not work, I did not test it.
var express = require('express'),
    app = express(),
    exphbs = require('express-handlebars'),
    hbs,
    path = require('path');

// serve all files under the /assets folder as static files
app.use('/assets', express.static(path.join(__dirname, '/assets')));

// handlebar engine config
hbs = exphbs.create({
    defaultLayout: 'main'
});

// attach engine and specify view location
app.engine('handlebars', hbs.engine);
app.set('view engine', 'handlebars');
app.set('views', path.join(__dirname, '/views'));

// home page http://domain.com/
app.get('/', function (req, resp) {
    resp.render('home', {title: 'Home | Hello World!', text: 'Welcome to my site!'});
});

// start webserver
app.listen(3000);

the above node app would create a webserver listening on port 3000 that responds to requests to /assets and to /. When / is requested, the home.handlebars view from the /views folder would be rendered using the main.handlebars layout from the /views/layouts Here's an example view that would display the title that was passed in for the / route created above:

上面的节点应用程序将创建一个侦听端口3000的Web服务器,它响应对/ assets和/的请求。当/请求时,/ views文件夹中的home.handlebars视图将使用/ views / layouts中的main.handlebars布局呈现。这是一个示例视图,它将显示为上面创建的/ route传递的标题:

/views/layouts/main.handlebars

/views/layouts/main.handlebars

<!doctype html>
<html>
<head>
    <meta charset="utf-8" />
    <title>{{title}}</title>
</head>
<body>
    {{{body}}}
</body>
</html>

/views/home.handlebars

/views/home.handlebars

<h1>Hello World!</h1>
<p>{{text}}</p>

#1


3  

You seem to be conflating two different features of PHP:

您似乎在混淆PHP的两个不同功能:

  1. Many web servers can be configured to run PHP programs through a PHP interpreter and serve the results to the browser.
  2. 许多Web服务器可以配置为通过PHP解释器运行PHP程序,并将结果提供给浏览器。
  3. PHP is designed as a template language with bells on, so PHP code is embedded in a template.
  4. PHP被设计为带有bells的模板语言,因此PHP代码嵌入在模板中。

If you are using Node.js then you will typically:

如果您使用的是Node.js,那么通常会:

  1. Write your webserver in Node.js (although you might configure a front end proxy for it). There is an example of this on the Node.js homepage, but there are also various frameworks, such as express, which do a lot of the heavy lifting for you.
  2. 在Node.js中编写您的Web服务器(尽管您可以为它配置前端代理)。在Node.js主页上有一个这样的例子,但也有各种框架,比如express,它为你做了很多繁重的工作。
  3. Keep your template code separate from your program code. Node has many template modules available for it.
  4. 将模板代码与程序代码分开。 Node有许多可用的模板模块。

Quoted from the Node.js homepage:

引自Node.js主页:

An example: Webserver

This simple web server written in Node responds with "Hello World" for every request.

用Node编写的这个简单的Web服务器响应每个请求的“Hello World”。

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');

To run the server, put the code into a file example.js and execute it with the node program from the command line:

要运行服务器,请将代码放入example.js文件中,并使用命令行中的节点程序执行它:

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

Here is an example of a simple TCP server which listens on port 1337 and echoes whatever you send it:

下面是一个简单的TCP服务器示例,该服务器侦听端口1337并回显您发送的任何内容:

var net = require('net');

var server = net.createServer(function (socket) {
  socket.write('Echo server\r\n');
  socket.pipe(socket);
});

server.listen(1337, '127.0.0.1');

#2


0  

The php code that you have been writing likely is only the html template version of php (for lack of better terms...)

您编写的PHP代码可能只是php的html模板版本(缺少更好的术语......)

When a .php page is requested in the browser, a php interpreter is invoked that parses the php tags in the html and replaces it with html/text. That result is then sent to the browser.

当在浏览器中请求.php页面时,调用php解释器来解析html中的php标记并用html / text替换它。然后将该结果发送到浏览器。

node.js doesn't work that way.

node.js不能那样工作。

Node.js is a lot more.... verbose than php when it comes to this specific topic. node.js is FAR more than just a web application framework or webserver, it can also be used as an executable of sorts to run common tasks.

当涉及到这个特定主题时,Node.js比php更详细。 node.js不仅仅是一个Web应用程序框架或Web服务器,它还可以用作运行常见任务的各种可执行文件。

typically, to get the kind of functionality you are looking for in node.js, you would use a templating framework such as handlebars along with express to handle the webserver and routing. Here's an example:

通常,要获得node.js中要查找的功能,您可以使用模板框架(如handlebars)和express来处理Web服务器和路由。这是一个例子:

// this is just an example, it may or may not work, I did not test it.
var express = require('express'),
    app = express(),
    exphbs = require('express-handlebars'),
    hbs,
    path = require('path');

// serve all files under the /assets folder as static files
app.use('/assets', express.static(path.join(__dirname, '/assets')));

// handlebar engine config
hbs = exphbs.create({
    defaultLayout: 'main'
});

// attach engine and specify view location
app.engine('handlebars', hbs.engine);
app.set('view engine', 'handlebars');
app.set('views', path.join(__dirname, '/views'));

// home page http://domain.com/
app.get('/', function (req, resp) {
    resp.render('home', {title: 'Home | Hello World!', text: 'Welcome to my site!'});
});

// start webserver
app.listen(3000);

the above node app would create a webserver listening on port 3000 that responds to requests to /assets and to /. When / is requested, the home.handlebars view from the /views folder would be rendered using the main.handlebars layout from the /views/layouts Here's an example view that would display the title that was passed in for the / route created above:

上面的节点应用程序将创建一个侦听端口3000的Web服务器,它响应对/ assets和/的请求。当/请求时,/ views文件夹中的home.handlebars视图将使用/ views / layouts中的main.handlebars布局呈现。这是一个示例视图,它将显示为上面创建的/ route传递的标题:

/views/layouts/main.handlebars

/views/layouts/main.handlebars

<!doctype html>
<html>
<head>
    <meta charset="utf-8" />
    <title>{{title}}</title>
</head>
<body>
    {{{body}}}
</body>
</html>

/views/home.handlebars

/views/home.handlebars

<h1>Hello World!</h1>
<p>{{text}}</p>