使用express.js路由设置对应用程序做出反应

时间:2017-09-10 05:04:46

标签: javascript node.js reactjs express

我正在使用angular.js,我在express.js

中这样做
app.get("*", function (req, res) {
  res.redirect('/#' + req.originalUrl)
})

这样浏览器将使用angular而不是express的路由。但是如何用反应路由器做到这一点?我有2个文件夹,名为服务器和客户端,服务器文件夹有express和api逻辑,而客户端文件夹只是一个反应应用程序。

1 个答案:

答案 0 :(得分:0)

您需要将要呈现应用的HTML文件的路径放入

app.get("/",(res,res) => {
res.sendFile('put the path of the html file you are rendering your app into here');
}

这是一个使用react

的express server.js的示例
var express = require('express');
var bodyParser = require('body-parser');
var logger = require('morgan');

var app = express();
var PORT = process.env.PORT || 3000;

app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.text());
app.use(bodyParser.json({type: 'application/vnd.api+json'}));

app.use(express.static('./public'));

app.get('/', function(req,res){
    res.sendFile('./public/index.html');
});


app.listen(PORT, function(){
    console.log("Listening on port: " +  PORT);
});
相关问题