Job Recruitment Website - Job seeking and recruitment - Node.js code to php

Node.js code to php

If your development team is using PHP and considering migrating to Node.js, then this article is suitable for you. This paper does not discuss the details of porting PHP to Node.js and the basic knowledge of Node.js. It covers: decision-making, starting point description, deep consideration of writing Node.js server, and deployment strategy.

Why did you move?

1stdibs decided to migrate from Apache/PHP to Node.js+Express for five reasons:

Less code

Full stack JS

Developers are happier.

mercantile rate of return

Future optimization

Less code

1stdibs is based on service-oriented architecture (SAO), and the front end calls Java services in the background. This means that the front-end model needs to be maintained at the same time, as well as the server PHP and client JS templates. Imagine that if you can get rid of PHP, you can unify the front-end rendering and background model in one language: JavaScript (and you can merge some templates at the same time). From the maintenance point of view, the code is more concise and has no repetitive logic.

Long live isomorphic JavaScript!

Full stack JS (and its advantages)

It is easy to use one language for the entire development stack. For developers, less environment switching makes them happy and efficient. The added benefit is that these tools are easier to use. Compared with the previous two package managers Composer and npm, only one is needed now. Although Composer is excellent, nbp is always necessary because it is responsible for tool and client management. Once all PHP code is removed, nbp will become the only package manager.

Developers are very happy.

It is very important for us to ensure that the skills of developers are expanded and their careers are continuously developed. For JavaScript engineers, Node.js is very attractive. It is very convenient and efficient to use the same tools, styles and modes on the server side as on the client side. In addition, Node.js is also quite popular and has made great progress in enterprise development. Node.js is a necessary skill for JavaScript engineers.

mercantile rate of return

We spent a lot of money to recruit excellent JS engineers and train junior JS engineers. Due to the complexity of the client stack, we need a senior JavaScript engineer. We no longer hire PHP engineers, only JavaScript engineers. Our point is, why not cultivate their skills on the server side?

Future optimization

In the long run, we intend to divide two huge applications into a series of small applications that are deployed independently. This can be easily achieved through Node.js, Express and nbp. In theory, PHP (for example, using Slim) can also do this. But instead of getting the above benefits, we will make a mess: the operation on Apache/PHP will be more complicated and the infrastructure will become a bit strange.

Selection framework

The PHP application that we finally replaced with Node.js mainly has the following responsibilities:

Login and authorization

Routing and server template engine (service HTML)

Boot front-end applications

Proxy service (avoiding CORS)

Service static resources (js, css, images)

These are the basic functions that we need to replace.

We have tried many frameworks, and Express is amazing (try the spreadsheet we evaluated). Any framework that is not based on Express looks unreliable. Express is easy to understand and well documented. In addition, you can recruit people who have been trained by Express.

We added some core modules of kraken (express-enrouten is used for routing and lusca is responsible for security); In addition, i 18n-node provides internationalization support, and the template engine uses Swig (we later gave up Swig. Hehe, open source software is still risky).

We have considered using kraken completely, but switching from the original server-side php template engine Twig to Swig is direct and fast. Besides, the dust and i 18n in craken are also unpleasant.

Write server

After choosing the framework, it's time to write the server.

When using Apache+PHP, you don't need to write another server. Apache itself is a server and PHP is an application. If you use Node.js, the server and the application are the same. From Apache/PHP to PHP, it is very important that you need to deal with some functions that you used naturally before. With Apache, you (or the system administrator) configure the server, and you don't have to care about what Apache does for you in the PHP application. Node.js works in different ways.

Provide static file service

There is no doubt that providing static file services is the core function of Apache. Node.js is different, you must configure the static file service in your application. Fortunately, it's simple, well documented and implemented in Express.

magazine

Many basic Apache configurations provide you with access logs and error logs. When using Node.js, as you might guess, it also needs to be configured in the application. Fortunately, many excellent open source software packages make it very simple. Morgan is a basic request recorder, which is simple in configuration and allows you to write logs to an output stream (standard output device or file). If you need to write logs to the database, or have other (more advanced) logging requirements, then try winston.

agency

We have a basic requirement: to be able to proxy ajax requests from clients to background services. It is much simpler to proxy all requests from the same domain than to process CORS headers. But if you want to use webpack-dev-server through proxy (as we did), you must deal with this problem in the Node.js application. Http-proxy is a simple and reliable solution.

The rest of the work

In addition to the above, there are many other tasks to be completed. Let's talk about an MVC application, which provides services for a series of single-page applications based on CodeIgniter(CI) framework. Most of the work is transplantation:

CI controller is ported to fast route selector and middleware (including login and authentication).

The Twig template engine is ported to Swig (this step is simple).

Service layer data access (in order to start the client single-page application normally)

The key components of the CodeIgniter model are not listed above. In fact, there is no need to rewrite the PHP model! That's great. Our client applications use the backbone model. Of course, this will lengthen the spine. Model.sync so that it can work globally on both the server and the client.

deploy

If your application is large, you shouldn't go online all at once. You can go online step by step through gradual deployment. It took us a few weeks.

Advantages of incremental deployment:

Minimize bug scope

Every time some routes and functions are released, other engineers can develop them normally.

Minimal impact on ongoing feature development and improvement-new features can continue to be released (which may lead to duplication of work)

If you do it properly, you can quickly roll back to the previous service.

NGINX is very good

How to go online step by step? We chose Nginx among many servers.

1

2

three

four

five

+ - +

http | |-& gt;

Apache/PHP

Request-> Nginx

|

| |-& gt;

Node.js

+ - +

Nginx allows you to "open" only one route at a time (turn it off if something goes wrong-we have encountered it many times), which gives you a lot of freedom. We also found it helpful to open the route without deploying the code. This provides us with some leeway in the weekly release plan.

However, there is one drawback. You need to ensure that the client code accepts the services provided by the old Apache/PHP server and the new Node.js server. This is not terrible, but you must transplant the unoptimized functions on the old server to the new server. Hold your breath and do it (remember to write a post-it note to clean up your technical debt).

abstract

From the beginning to the end, the whole transplant work took about one year. This may sound ridiculous, but this timetable includes the decision-making process (in a hurry), writing the core framework to meet the requirements based on Express, porting all functions and going online step by step. Besides, remember that we always have only one or two developers working for it-and part-time.

If you want to have a try, please think carefully. Can your team benefit? Can your whole organization benefit? If you come from a business organization, please remember that business needs to keep running. You need to find a good balance between business goals and engineering goals.