我将如何使用Node.js作为后端和Wordpress作为后端?

时间:2021-10-03 16:25:31

I've had a thought of using Wordpress as a CMS backend, because well a lot of people know it and it is easy to use and then using Node.JS as the front-end. You're probably thinking now why would I want to do that in the first place, what is the advantage?

我曾想过使用Wordpress作为CMS后端,因为很多人都知道它并且很容易使用,然后使用Node.JS作为前端。您现在可能在想,为什么我想首先想要这样做,有什么好处?

I want to use websockets and the wonderful Socket.io library for Node.JS provides beautiful cross-browser websockets support. Essentially I want a user to come to a site, a websocket is created and then content is fed to the frontend asynchronously as JSON and then decoded on the frontend all without page refreshing.

我想使用websockets和Node.JS的精彩Socket.io库提供了漂亮的跨浏览器websockets支持。基本上我希望用户来到一个站点,创建一个websocket,然后将内容作为JSON异步地提供给前端,然后在前端进行解码,而不进行页面刷新。

Effectively I am making Wordpress become a real-time CMS. You visit a site, but every link you click fetches the page as JSON and returns it via a websocket to save multiple requests and of course, page size.

实际上,我正在使Wordpress成为一个实时CMS。您访问某个站点,但您单击的每个链接都将该页面作为JSON提取并通过websocket返回以保存多个请求,当然还有页面大小。

How do I go about getting Node.JS talking to a MySQL database, pulling out info and then showing it? Any tutorials, resources and other useful tips would be gratefully appreciated. A few of my colleagues have wondered the same thing, so I think the answers will be a big help to everyone.

我如何让Node.JS与MySQL数据库交谈,提取信息然后显示?任何教程,资源和其他有用的提示将不胜感激。我的一些同事也想知道同样的事情,所以我认为答案对每个人都有很大的帮助。

4 个解决方案

#1


9  

To be exact, you can't use Node.js for a front-end solution, since it runs on the server, not the browser (think of it like any other server-side language such as PHP, JSP etc).

确切地说,您不能将Node.js用于前端解决方案,因为它在服务器上运行,而不是在浏览器上运行(将其视为任何其他服务器端语言,如PHP,JSP等)。

You can, however, create the described solution with jQuery or any other Javascript library, you just have to implement data transfer with Socket.IO. On the server-side you'd need something to handle websockets, so the most native way would be to use Node.js, but since you want to use Wordpress, it gets really complicated, as Wordpress is not meant to be used in the way you described, so I'm afraid you'd have to write your CMS from ground up in Node.

但是,您可以使用jQuery或任何其他Javascript库创建所描述的解决方案,您只需使用Socket.IO实现数据传输。在服务器端你需要一些东西来处理websockets,所以最本地的方式是使用Node.js,但由于你想使用Wordpress,它变得非常复杂,因为Wordpress并不打算用于你描述的方式,所以我担心你必须在Node中从头开始编写你的CMS。

Also, the way you described has a huge flaw. Search engine crawlers are still unable to parse and run Javascript, so if all of your content is loaded dynamically, it would seem empty to Google and others, so it would be impossible to ever make it in the search results rendering your site pretty much useless.

而且,你描述的方式有一个巨大的缺陷。搜索引擎抓取工具仍然无法解析和运行Javascript,因此如果您的所有内容都是动态加载的,那么谷歌和其他人似乎都是空的,因此无法在搜索结果中使其显示您的网站几乎无用。

For MySQL and other modules for Node, you should check NPM registry and the Node modules page.

对于MySQL和Node的其他模块,您应该检查NPM注册表和Node模块页面。

EDIT After Dwayne explained his solution in comments, this is how I'd do it:

编辑在Dwayne在评论中解释他的解决方案后,我就是这样做的:

  1. I'd use jQuery for front-end. Binding the document with .on(), and setting the selector to 'a', so that every anchor on the webpage would fire the handler.
  2. 我将jQuery用于前端。使用.on()绑定文档,并将选择器设置为“a”,以便网页上的每个锚点都会触发处理程序。

  3. The handler parses the a.href attribute and figures out whether it's an external link, which shouldn't be handled by Javascript, or if it's a link to the next page, to an article etc. You can prevent the default action by calling e.preventDefault() in the handler, which prevents the browser from redirecting to the location.
  4. 处理程序解析a.href属性并确定它是否是外部链接,不应由Javascript处理,或者它是指向下一页,文章等的链接。您可以通过调用e来阻止默认操作处理程序中的.preventDefault(),可防止浏览器重定向到该位置。

  5. Then the handler would get the content in JSON by calling .getJSON() to the URL based on the article. The easiest way would be to have a certain pattern (such as all urls like www.domain.com/api) redirect to the Node service via .htaccess, to prevent cross-domain problems.
  6. 然后处理程序将通过基于文章调用.getJSON()来获取JSON中的内容。最简单的方法是使某个模式(例如所有网址,如www.domain.com/api)通过.htaccess重定向到Node服务,以防止跨域问题。

  7. Node would then see the request, extract the parameters and figure out what the user wants. Then connect to the MySQL database with this module (it's as simple as it can get) and return the corresponding content formatted as JSON. Don't forget to set the Content-Type headers to 'application/json'.
  8. 然后节点将看到请求,提取参数并找出用户想要的内容。然后使用此模块连接到MySQL数据库(它尽可能简单)并返回格式为JSON的相应内容。不要忘记将Content-Type标头设置为'application / json'。

  9. jQuery gets the response, figure out the type of the request and updates the content accordingly. Profit.
  10. jQuery获取响应,找出请求的类型并相应地更新内容。利润。

As you can see, I wouldn't use WebSockets in this case, since you wouldn't really benefit much from it. They are mostly meant for small real-time updates (no huge HTTP headers to reduce the bandwidth) that are both-ways. This means that the server could also push data into the browser, without the browser asking for it. In a blog context, this is not required, and you won't have too many request, so the difference in bandwidth wouldn't be noticeable anyway. If, however, you would like to use it for educational purposes, just basically replace the getJSON part with SocketIO, I'm not sure whether Apache supports proxying WebSockets, though. Extra information about SocketIO basics are here.

正如您所看到的,在这种情况下我不会使用WebSockets,因为您不会从中获益太多。它们主要用于两种方式的小型实时更新(没有巨大的HTTP标头来减少带宽)。这意味着服务器也可以将数据推送到浏览器中,而浏览器不需要它。在博客上下文中,这不是必需的,并且您不会有太多请求,因此带宽的差异无论如何都不会引人注意。但是,如果你想将它用于教育目的,基本上用SocketIO替换getJSON部分,我不确定Apache是​​否支持代理WebSockets。有关SocketIO基础知识的更多信息,请点击此处。

#2


2  

Edit: I overlooked the part with 'using Node.js on the front-end'. As Vahur Roosimaa said, Node.js is on the server-side (think of it as Nginx / Apache + PHP combination). Node isn't a frontend library like jQuery. If you want you can use it just for the websockets functionality (I suggest using Socket.IO).

编辑:我忽略了“在前端使用Node.js”的部分。正如Vahur Roosimaa所说,Node.js位于服务器端(将其视为Nginx / Apache + PHP组合)。 Node不是像jQuery那样的前端库。如果你想要,你只能用于websockets功能(我建议使用Socket.IO)。

Nice tutorials about Node.js and MySQL:

关于Node.js和MySQL的好教程:

http://www.giantflyingsaucer.com/blog/?p=2596
http://mclear.co.uk/2011/01/26/very-simple-nodejs-mysql-select-query-example/
http://www.hacksparrow.com/using-mysql-with-node-js.html

http://www.giantflyingsaucer.com/blog/?p=2596 http://mclear.co.uk/2011/01/26/very-simple-nodejs-mysql-select-query-example/ http:// www.hacksparrow.com/using-mysql-with-node-js.html

This SO question might also help: MySQL with Node.js

这个问题也可能有所帮助:带有Node.js的MySQL

Also check the examples from the github repo of node-mysql.

还要查看node-mysql的github repo中的示例。

If you want something more advanced like an ORM, I recommend Sequelize.
Another good question from SO: Which ORM should I use for Node.js and MySQL?

如果你想要像ORM这样更先进的东西,我推荐Sequelize。来自SO的另一个好问题:我应该将哪些ORM用于Node.js和MySQL?

#3


2  

You should check out Wordscript which I recently added a Node JS example which can act as a simple front end for doing basic post retrieval from a Wordpress database.

您应该查看我最近添加了一个Node JS示例的Wordscript,它可以作为从Wordpress数据库进行基本帖子检索的简单前端。

It uses a common mysql library for node, and generates MySQL queries from get parameters and renders data as it is retrieved from the database; including tags.

它为节点使用一个通用的mysql库,并从get参数生成MySQL查询,并在从数据库中检索数据时呈现数据;包括标签。

Wordscript aims to free backend/frontend developers from being forced to work with the Wordpress PHP codebase, but still allows for Wordpress'es administrative interface to be used when needed (and prudent to do so). API's have been written in Ruby and PHP that both return JSON feeds and function generally the same way the node version does; so thats an additional option where a scripting language is available.

Wordscript旨在释放后端/前端开发人员*使用Wordpress PHP代码库,但仍然允许在需要时使用Wordpress'es管理界面(并谨慎地这样做)。 API已经用Ruby和PHP编写,它们都返回JSON提要和函数,通常与节点版本相同;这是脚本语言可用的附加选项。

#4


0  

One option you have, if you want to have wordpress as the CMS and keep its admin UI, is to write your wordpress templates to output JSON instead of HTML.

如果你想将wordpress作为CMS并保留其管理界面,你有一个选择就是编写你的wordpress模板来输出JSON而不是HTML。

In contrast to Wordscript, this is more solution specific, since you will need to write your JSON output for every template/data you want. The upside is that you can create the JSON specifically for your needs.

与Wordscript相比,这更具体解决方案,因为您需要为所需的每个模板/数据编写JSON输出。好处是您可以根据需要创建JSON。

On the node side, you write a small server that will consume the JSON, letting you use whatever javascript template language you want. Nodejs will also help out with performance, since you can save the rendered content and/or the JSON output in memory, saving you roundtrips to the wordpress templates.

在节点方面,您编写了一个将使用JSON的小型服务器,允许您使用您想要的任何JavaScript模板语言。 Nodejs还可以帮助提高性能,因为您可以将渲染的内容和/或JSON输出保存在内存中,从而节省了对wordpress模板的往返。

I wrote a blog about this, which describes more of the benefits of using nodejs and wordpress together.

我写了一篇关于此的博客,它描述了使用nodejs和wordpress的更多好处。

http://www.1001.io/improve-wordpress-with-nodejs/

#1


9  

To be exact, you can't use Node.js for a front-end solution, since it runs on the server, not the browser (think of it like any other server-side language such as PHP, JSP etc).

确切地说,您不能将Node.js用于前端解决方案,因为它在服务器上运行,而不是在浏览器上运行(将其视为任何其他服务器端语言,如PHP,JSP等)。

You can, however, create the described solution with jQuery or any other Javascript library, you just have to implement data transfer with Socket.IO. On the server-side you'd need something to handle websockets, so the most native way would be to use Node.js, but since you want to use Wordpress, it gets really complicated, as Wordpress is not meant to be used in the way you described, so I'm afraid you'd have to write your CMS from ground up in Node.

但是,您可以使用jQuery或任何其他Javascript库创建所描述的解决方案,您只需使用Socket.IO实现数据传输。在服务器端你需要一些东西来处理websockets,所以最本地的方式是使用Node.js,但由于你想使用Wordpress,它变得非常复杂,因为Wordpress并不打算用于你描述的方式,所以我担心你必须在Node中从头开始编写你的CMS。

Also, the way you described has a huge flaw. Search engine crawlers are still unable to parse and run Javascript, so if all of your content is loaded dynamically, it would seem empty to Google and others, so it would be impossible to ever make it in the search results rendering your site pretty much useless.

而且,你描述的方式有一个巨大的缺陷。搜索引擎抓取工具仍然无法解析和运行Javascript,因此如果您的所有内容都是动态加载的,那么谷歌和其他人似乎都是空的,因此无法在搜索结果中使其显示您的网站几乎无用。

For MySQL and other modules for Node, you should check NPM registry and the Node modules page.

对于MySQL和Node的其他模块,您应该检查NPM注册表和Node模块页面。

EDIT After Dwayne explained his solution in comments, this is how I'd do it:

编辑在Dwayne在评论中解释他的解决方案后,我就是这样做的:

  1. I'd use jQuery for front-end. Binding the document with .on(), and setting the selector to 'a', so that every anchor on the webpage would fire the handler.
  2. 我将jQuery用于前端。使用.on()绑定文档,并将选择器设置为“a”,以便网页上的每个锚点都会触发处理程序。

  3. The handler parses the a.href attribute and figures out whether it's an external link, which shouldn't be handled by Javascript, or if it's a link to the next page, to an article etc. You can prevent the default action by calling e.preventDefault() in the handler, which prevents the browser from redirecting to the location.
  4. 处理程序解析a.href属性并确定它是否是外部链接,不应由Javascript处理,或者它是指向下一页,文章等的链接。您可以通过调用e来阻止默认操作处理程序中的.preventDefault(),可防止浏览器重定向到该位置。

  5. Then the handler would get the content in JSON by calling .getJSON() to the URL based on the article. The easiest way would be to have a certain pattern (such as all urls like www.domain.com/api) redirect to the Node service via .htaccess, to prevent cross-domain problems.
  6. 然后处理程序将通过基于文章调用.getJSON()来获取JSON中的内容。最简单的方法是使某个模式(例如所有网址,如www.domain.com/api)通过.htaccess重定向到Node服务,以防止跨域问题。

  7. Node would then see the request, extract the parameters and figure out what the user wants. Then connect to the MySQL database with this module (it's as simple as it can get) and return the corresponding content formatted as JSON. Don't forget to set the Content-Type headers to 'application/json'.
  8. 然后节点将看到请求,提取参数并找出用户想要的内容。然后使用此模块连接到MySQL数据库(它尽可能简单)并返回格式为JSON的相应内容。不要忘记将Content-Type标头设置为'application / json'。

  9. jQuery gets the response, figure out the type of the request and updates the content accordingly. Profit.
  10. jQuery获取响应,找出请求的类型并相应地更新内容。利润。

As you can see, I wouldn't use WebSockets in this case, since you wouldn't really benefit much from it. They are mostly meant for small real-time updates (no huge HTTP headers to reduce the bandwidth) that are both-ways. This means that the server could also push data into the browser, without the browser asking for it. In a blog context, this is not required, and you won't have too many request, so the difference in bandwidth wouldn't be noticeable anyway. If, however, you would like to use it for educational purposes, just basically replace the getJSON part with SocketIO, I'm not sure whether Apache supports proxying WebSockets, though. Extra information about SocketIO basics are here.

正如您所看到的,在这种情况下我不会使用WebSockets,因为您不会从中获益太多。它们主要用于两种方式的小型实时更新(没有巨大的HTTP标头来减少带宽)。这意味着服务器也可以将数据推送到浏览器中,而浏览器不需要它。在博客上下文中,这不是必需的,并且您不会有太多请求,因此带宽的差异无论如何都不会引人注意。但是,如果你想将它用于教育目的,基本上用SocketIO替换getJSON部分,我不确定Apache是​​否支持代理WebSockets。有关SocketIO基础知识的更多信息,请点击此处。

#2


2  

Edit: I overlooked the part with 'using Node.js on the front-end'. As Vahur Roosimaa said, Node.js is on the server-side (think of it as Nginx / Apache + PHP combination). Node isn't a frontend library like jQuery. If you want you can use it just for the websockets functionality (I suggest using Socket.IO).

编辑:我忽略了“在前端使用Node.js”的部分。正如Vahur Roosimaa所说,Node.js位于服务器端(将其视为Nginx / Apache + PHP组合)。 Node不是像jQuery那样的前端库。如果你想要,你只能用于websockets功能(我建议使用Socket.IO)。

Nice tutorials about Node.js and MySQL:

关于Node.js和MySQL的好教程:

http://www.giantflyingsaucer.com/blog/?p=2596
http://mclear.co.uk/2011/01/26/very-simple-nodejs-mysql-select-query-example/
http://www.hacksparrow.com/using-mysql-with-node-js.html

http://www.giantflyingsaucer.com/blog/?p=2596 http://mclear.co.uk/2011/01/26/very-simple-nodejs-mysql-select-query-example/ http:// www.hacksparrow.com/using-mysql-with-node-js.html

This SO question might also help: MySQL with Node.js

这个问题也可能有所帮助:带有Node.js的MySQL

Also check the examples from the github repo of node-mysql.

还要查看node-mysql的github repo中的示例。

If you want something more advanced like an ORM, I recommend Sequelize.
Another good question from SO: Which ORM should I use for Node.js and MySQL?

如果你想要像ORM这样更先进的东西,我推荐Sequelize。来自SO的另一个好问题:我应该将哪些ORM用于Node.js和MySQL?

#3


2  

You should check out Wordscript which I recently added a Node JS example which can act as a simple front end for doing basic post retrieval from a Wordpress database.

您应该查看我最近添加了一个Node JS示例的Wordscript,它可以作为从Wordpress数据库进行基本帖子检索的简单前端。

It uses a common mysql library for node, and generates MySQL queries from get parameters and renders data as it is retrieved from the database; including tags.

它为节点使用一个通用的mysql库,并从get参数生成MySQL查询,并在从数据库中检索数据时呈现数据;包括标签。

Wordscript aims to free backend/frontend developers from being forced to work with the Wordpress PHP codebase, but still allows for Wordpress'es administrative interface to be used when needed (and prudent to do so). API's have been written in Ruby and PHP that both return JSON feeds and function generally the same way the node version does; so thats an additional option where a scripting language is available.

Wordscript旨在释放后端/前端开发人员*使用Wordpress PHP代码库,但仍然允许在需要时使用Wordpress'es管理界面(并谨慎地这样做)。 API已经用Ruby和PHP编写,它们都返回JSON提要和函数,通常与节点版本相同;这是脚本语言可用的附加选项。

#4


0  

One option you have, if you want to have wordpress as the CMS and keep its admin UI, is to write your wordpress templates to output JSON instead of HTML.

如果你想将wordpress作为CMS并保留其管理界面,你有一个选择就是编写你的wordpress模板来输出JSON而不是HTML。

In contrast to Wordscript, this is more solution specific, since you will need to write your JSON output for every template/data you want. The upside is that you can create the JSON specifically for your needs.

与Wordscript相比,这更具体解决方案,因为您需要为所需的每个模板/数据编写JSON输出。好处是您可以根据需要创建JSON。

On the node side, you write a small server that will consume the JSON, letting you use whatever javascript template language you want. Nodejs will also help out with performance, since you can save the rendered content and/or the JSON output in memory, saving you roundtrips to the wordpress templates.

在节点方面,您编写了一个将使用JSON的小型服务器,允许您使用您想要的任何JavaScript模板语言。 Nodejs还可以帮助提高性能,因为您可以将渲染的内容和/或JSON输出保存在内存中,从而节省了对wordpress模板的往返。

I wrote a blog about this, which describes more of the benefits of using nodejs and wordpress together.

我写了一篇关于此的博客,它描述了使用nodejs和wordpress的更多好处。

http://www.1001.io/improve-wordpress-with-nodejs/