我使用Node with Express创建了一个应用程序。当我发出get请求时,它返回从节点服务器呈现的html文件。 我使用route.Hnd渲染了html文件,并继续为每个get请求发送html文件。
节点应用程序的代码
import express from 'express';
import mongoose from 'mongoose';
import bodyParser from 'body-parser';
import passport from 'passport';
import cros from 'cors';
import path from 'path';
import webpack from 'webpack';
import webpackmiddleware from 'webpack-dev-middleware';
import webpackHotMiddleware from 'webpack-hot-middleware';
import webpackConfig from '../webpack.config.dev';
import regusers from './models/regusers.model';
import Router from './routes/UserRouter';
const cross = cros();
const app = express();
const router = express.Router();
const port =3000;
const compile=webpack(webpackConfig);
const db='mongodb://localhost/parkspace';
mongoose.Promise = global.Promise;
mongoose.connect(db);
app.use(webpackmiddleware(compile,{
hot:true,
publicPath: webpackConfig.output.publicPath,
noInfo:true
}));
app.use(webpackHotMiddleware(compile));
app.use(cross);
app.use(router);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended:true
}));
app.get('/*',(req,res) => {
res.sendFile(path.join(__dirname,'./index.html'));
});
Router(app);
app.listen(port,()=> console.log('Running on port: '+port));
我的路由器文件
import Authentication from '../auth/auth';
import passportService from '../services/passport';
import passport from 'passport';
const requireAuth = passport.authenticate('jwt',{session:false});
const user = (app) => {
app.get('/',requireAuth,function (req,res){
res.send({hi:'there'});
});
app.post('/signup',Authentication.signup);
}
export default user;
如何克服这个问题?
答案 0 :(得分:2)
只需将index.html
放在渲染Router(app);
app.get('/*',(req,res) => {
res.sendFile(path.join(__dirname,'./index.html'));
});
逻辑之前:
Router.js
但您在/auth
中获取XHR请求的路径应该更具体,例如req.xhr
。您还可以检查fmt.Println
以区分请求是来自XHR还是只是渲染请求。
答案 1 :(得分:0)
我认为这种情况正在发生,因为在你的路线中你有一个通配符/*
,所以无论你去哪里,都会发送html文件。
希望这有帮助。