How to run old nodejs Project into new nodejs project
Tags: node, pm2, run
Published: 2023-04-09 17:59:46

To run two Node.js projects in one project, you need to follow these steps:

Navigate to the root directory of your first Node.js project in the command line.

Run the following command to install all the dependencies listed in the first project's package.json file:

`npm install`

Run the first project using the following command:

`node app.js`

Note: Replace "app.js" with the name of the main file of your first project.

Open a new command prompt or terminal window and navigate to the root directory of your second Node.js project.

Repeat steps 2 and 3 for your second project.

Now you have two Node.js projects running simultaneously. However, they are not integrated with each other yet.

To integrate the two projects, you need to create a new file that will act as the entry point for both projects. This file will import the main files of both projects and start them together.

Create a new file named "server.js" or any other name you prefer.

In the new file, import the main files of both projects and start them together. Here is an example:

```
const app1 = require('./first-project/app.js');
const app2 = require('./second-project/app.js');

app1.listen(3000, () => console.log('First project running on port 3000'));
app2.listen(4000, () => console.log('Second project running on port 4000'));
```
Note: Replace "./first-project/app.js" and "./second-project/app.js" with the paths to the main files of your first and second projects, respectively. Also, replace the port numbers with the ports that you want to use for each project.

Save the new file and run it using the following command:

```
node server.js
```
Now both projects should be running together. You can access them by going to "http://localhost:3000" for the first project and "http://localhost:4000" for the second project.

Test both projects thoroughly to ensure that everything is working as expected.


**Step You have to follow**
1. Install the http-proxy-middleware module in your Node.js project using the following command:

`npm install http-proxy-middleware`

2. In the main file of your first Node.js project, import the http-proxy-middleware module:

`const { createProxyMiddleware } = require('http-proxy-middleware');`

3. Create a proxy middleware for your second Node.js project: 

`const secondProjectProxy = createProxyMiddleware({
  target: 'http://localhost:4000', // Replace with the address of your second project
  changeOrigin: true,
});
`

4. In the same main file, create a new instance of http.Server:

`const server = require('http').createServer();`

5. Add a listener for the server's request event:

```
server.on('request', (req, res) => {
  if (req.url.startsWith('/second-project')) {
    // Use the proxy middleware for requests to the second project
    secondProjectProxy(req, res);
  } else {
    // Handle requests for the first project
    // ...
  }
});
```
This code checks if the request URL starts with "/second-project". If it does, the request is forwarded to the second project using the proxy middleware. Otherwise, the request is handled by the first project.

6. Start the server and listen on the desired port:

```
server.listen(3000, () => {
  console.log('Server running on port 3000');
});
```
7. In your second project, you need to make sure that it is configured to listen on a different port, such as 4000 in this example.

With this setup, both Node.js projects are running on separate ports but can be accessed on the same port using the first project's server. Requests to the first project are handled directly by its own code, while requests to the second project are forwarded to it via the proxy middleware.