DEV Community

Himanshu Gupta
Himanshu Gupta

Posted on

How to run old nodejs Project into new nodejs project

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'));
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

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

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

  1. 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,
});

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

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

  1. 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
    // ...
  }
});
Enter fullscreen mode Exit fullscreen mode

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.

  1. Start the server and listen on the desired port:
server.listen(3000, () => {
  console.log('Server running on port 3000');
});
Enter fullscreen mode Exit fullscreen mode
  1. 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.

Top comments (1)

Collapse
 
gottz profile image
Jan-Stefan Janetzky

this only works for somewhat similar base versions..
it is always a great idea to have tests, that can validate upgrades so.. I'd suggest testability before Integration.
especially if you rely on modules that pollute globalThis etc.