Why is my react router not passing my express route to server?

大憨熊 提交于 2019-12-13 05:16:20

问题


I tried to add social login to my (already working) react/express app, and I got it working in localhost. However, when I deploy it to production, the social login doesn't work. This is how it gets started

<a href="/api/auth/google">Google+</a>

However, in production, it stays at https://sample.com/api/auth/google in my browser. So, it appears the react router is catching it first before express. How?

In localhost, it works because the proxy in package.js

"proxy": {
    "/api": {
      "target": "http://localhost:4000",
      "ws": true
    }

Now, how can I do this for production?

By the way, all my server APIs starts with '/api/...'. Also, in my react routes, I don't have a catch-all component.

UPDATE: Here is my server.js

var express = require('express');
var session = require('express-session');
var bodyParser = require('body-parser');
var flash = require('connect-flash');
var cookieParser = require('cookie-parser');
var path = require('path');
var fs = require('fs');

var app = express();
var isSecured = true;
app.isDevMode = app.get('env') == 'development'

require('./server/config/log')(app)

var port = (process.env.PORT || app.isDevMode) ? 4000 : (isSecured ? 443 : 80);
app.set('port', port);

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json({ limit: '10mb' }));

app.use(flash()); // use connect-flash for flash messages stored in session

var server = require('http').createServer(app);
if (!app.isDevMode && isSecured) {
  var options = {
    ca: fs.readFileSync('ca_bundle.crt'),
    key: fs.readFileSync('private.key'),
    cert: fs.readFileSync('certificate.crt')
  }
  server = require('https').createServer(options, app);
}

db.on('error', console.error.bind(console, 'connection error:'));

db.once('open', function () {
  console.log('Connected to MongoDB');

  var routes = require('./server/routes');
  routes.init(app);

  if (app.isDevMode) {
    app.use(express.static(__dirname));
  }
  else {
    app.use(express.static(path.join(__dirname, 'client/build')));

    app.get('/*', function (req, res) {
      res.sendFile(path.join(__dirname, 'client/build', 'index.html'));
    });

    if (isSecured) {
      require('http').createServer(function (req, res) {
        res.writeHead(307, { "Location": "https://" + req.headers['host'] + req.url });
        res.end();
      }).listen(80);
    }
  }
  server.listen(app.get('port'), function () {
    console.log('Server listening on port ' + app.get('port'));
  });
});

module.exports = app;

Here is my routes:

app.get('/api/auth/google', passport.authenticate('google', { scope: ['https://www.googleapis.com/auth/userinfo.profile', 'https://www.googleapis.com/auth/userinfo.email'] }))
app.get('/api/callback/google', passport.authenticate('google', {successRedirect: '/?action=login&provider=google', failureRedirect: '/?action=login'}))

UPDATE: Here is the morgan log. I added a number for each line for my reference. Line 4 started when I click the link to send '/api/auth/google', and finished at line 6.

1. GET /api/get/list?parm={%22kind%22:%22Prod%22,%22limit%22:5,%22createdOn%22:-1} 304 - - 22.959 ms
2. GET /images/logo.png 200 3432 - 17.410 ms
3. GET /service-worker.js 200 3097 - 3.398 ms
4. GET /static/js/main.cef8cdac.js 304 - - 5.180 ms
5. GET /images/two.png 304 - - 4.908 ms
6. GET /service-worker.js 200 3097 - 3.838 ms

So, basically, the request didn't come to express server. Actually, if I had a catch all route in react, I can see it's hitting there.

Here is the network log:

来源:https://stackoverflow.com/questions/53056212/why-is-my-react-router-not-passing-my-express-route-to-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!