我的索引路由有效,但是我的app.get()函数默认为前端路由。我在前端使用React Router Dom。而且react可以调用这些路由,但是我无法将客户端路由到备份的路由。
app.js
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var cors = require('cors');
const session = require('express-session');
const passport = require('passport');
var index = require('./routes/index');
var app = express();
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
// Middle Ware
app.use(
session({
secret: "\x02\xf3\xf7r\t\x9f\xee\xbbu\xb1\xe1\x90\xfe'\xab\xa6L6\xdd\x8d[\xccO\xfe",
resave: false,
saveUninitialized: true
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(express.static(path.join(__dirname, 'client/build'))).use(cors());
app.get('/callback', passport.authenticate("spotify", {
failureRedirect: 'https://my-app.herokuapp.com/login'}),
(req, res, body) => {
res.redirect('https://my-app.herokuapp.com/'
);
}
);
app.use('/', index);
app.get('*', (req, res) => {
res.sendfile(__dirname + '/client/build/index.html');
});
module.exports = app;