How can I use Node.js for Frontend and Wordpress as a Backend?

I thought that Wordpress is a CMS backend, because many people know about it, and it is easy to use, and then use Node.JS as an interface. You are probably thinking now why I want to do this in the first place, what is the advantage?

I want to use websockets and the wonderful Socket.io library for Node.JS provides cross-browser support. In fact, I want the user to come to the site, create a websocket, and then the content is transferred to the interface asynchronously, like JSON, and then decoded on the interface without refreshing the page.

In fact, I am doing Wordpress in real-time CMS. You visit the site, but each link you click selects the page as JSON and returns it through the website to save multiple requests and, of course, page size.

How do I start getting Node.JS by talking to a MySQL database, pulling out information and then showing it? Any tutorials, resources, and other helpful tips would be greatly appreciated. Some of my colleagues are asking the same question, so I think the answers will be a big help for everyone.

+4
source share
4 answers

To be precise, you cannot use Node.js for a front-end solution, as it runs on the server and not in the browser (think of it like any other server language such as PHP, JSP, etc.).

However, you can create the described solution using jQuery or any other Javascript library, you just need to perform data transfer using Socket.IO. On the server side, you need to handle something using websockets, so the most common way would be to use Node.js, but since you want to use Wordpress, it becomes very complicated because Wordpress is not intended to be used in the way you described, so I'm afraid you will have to write your CMS from scratch in Node.

In addition, the method described by you has a huge drawback. Search robots still cannot analyze and run Javascript, so if all your content was loaded dynamically, it would appear empty for Google and others, so it would be impossible to do this in the search results, which will make your site almost useless.

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

EDIT After Dwayne explained his decision in the comments, here's how I do it:

  • I would use jQuery for the front-end. Linking document c . On () and setting the selector to 'a' so that every anchor on the web page handler.
  • The handler parses the a.href attribute and calculates whether the external link is not to be processed by Javascript, or if it links to the next page, article, etc. You can prevent the default action by calling e.preventDefault() in the handler, which prevents the browser from being redirected to a location.
  • Then the handler will get the content in JSON by calling .getJSON() on the article-based URL. The easiest way is to have a specific structure (for example, all URLs like www.domain.com/api), redirect to the Node service using .htaccess to prevent problems with multiple domains.
  • Node will then see the request, extract the parameters and find out what the user wants. Then connect to the MySQL database using this module (as simple as possible) and return the appropriate content formatted as JSON. Remember to set the Content-Type headers to "application / json".
  • jQuery receives a response, calculates the type of request, and updates the content accordingly. Profit.

As you can see, I would not use WebSockets in this case, since you are unlikely to benefit from this. They are mainly intended for small, real-time updates (without huge HTTP headers to reduce bandwidth), which are two-way. This means that the server can also insert data into the browser without asking the browser. In the context of the blog, this is not required, and you will not have too many requests, so the difference in throughput will not be noticeable in any case. If, however, you would like to use it for educational purposes, just replace the getJSON part with SocketIO, I'm not sure if Apache supports proxies. Additional information on the basics of SocketIO here .

+9
source

Edit: I skipped the part using Node.js on the interface. As Vahur Roosimaa said, Node.js is on the server side (think of it as a combination of Nginx / Apache + PHP). Node is not a front-end library such as jQuery. If you want, you can use it only for websockets functionality (I suggest using Socket.IO ).

Good tutorials on Node.js and 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

This question may also help: MySQL with Node.js

Also check out the examples from the github node -mysql repository .

If you want something more advanced like ORM, I recommend Sequelize .

Another good question from SO: Which ORM should I use for Node.js and MySQL?

+2
source

You should check out the Wordscript , which I recently added Node JS , which can act as a simple interface to perform a basic search after writing from a Wordpress database.

It uses the common mysql library for node and generates MySQL queries from receiving parameters and displays data as it is received from the database; including tags.

Wordscript seeks to free backend / frontend developers from forced work with the Wordpress PHP codebase, but at the same time allows using the Wordpress administrative interface as necessary (and it is reasonable to do this). The APIs were written in Ruby and PHP that return JSON pipes and functions, usually in the same way as the version of node; so this is an additional option in which the scripting language is available.

+2
source

One option you have if you want Wordpress to be CMS and support its admin user interface is to write your Wordpress templates to display JSON instead of HTML.

Unlike Wordscript, this is more suitable for the solution, since you will need to write your JSON output for each template / data that you want. The good news is that you can create JSON specifically for your needs.

On the node side, you are writing a small server that will use JSON, allowing you to use any JavaScript template language. Nodejs will also help in performance since you can save the displayed content and / or JSON output in memory by storing callbacks in Wordpress templates.

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

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

0
source

Source: https://habr.com/ru/post/1386277/


All Articles