skip to Main Content

I have a PHP site https://example.com.
I have a MEAN stack application subdomain http://team.example.com. It uses APIs provided by nodejs on port 3000.

I’m facing a problem when running the application on http://team.example.com where the Nodejs API is not reachable .

added the following to Apache Config File:

ProxyPass /node/ http://localhost:3000/

I am sending api request from angular side with the following:

team.example.com/node/users/login

APIs reached successfully via postman , but fails on browser

How can I solve this problem?

2

Answers


  1. I think you have CORS issue, I’m assuming that you are using express framework in your node service.
    See the following sample code to know how to solve CORS issue for browser.

    var http = require('http');
    var express = require('express');
    var app = express();
    
    app.use(function(req, res, next) {
      res.header('Access-Control-Allow-Origin', "*");
      res.header('Access-Control-Allow-Methods','GET,PUT,POST,DELETE,OPTIONS'); 
      res.header('Access-Control-Allow-Headers', 'Content-Type'); 
      next();
    });
    
    app.post('/test-cors', function (req, res) {
      res.set('Content-Type', 'application/json');
      res.send(JSON.stringify({ 'status': "OK" }));
    });
    
    // Create http server and run it
    var server = http.createServer(app);
    
    server.listen(8081, function() {
        console.log("Listening on 8081");
    });
    

    In above sample code you need to focus on following code lines:

    app.use(function(req, res, next) {
      res.header('Access-Control-Allow-Origin', "*");
      res.header('Access-Control-Allow-Methods','GET,PUT,POST,DELETE'); 
      res.header('Access-Control-Allow-Headers', 'Content-Type'); 
      next();
    });
    
    Login or Signup to reply.
  2. Blockquote

    For using the proxy you have to enable the proxy module in apache. After that restart the apache.

    If you are using ubuntu os run following command

    sudo a2enmod proxy &&
    sudo a2enmod proxy_http

    After this, you have to run

    sudo service apach2 restart.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search