2017-09-10 40 views
2

我用angular.js,我這樣做是express.js反應應用與express.js路線設置

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

,使瀏覽器將使用的角度,而不是明確的路線。但如何與反應路由器做到這一點?我有2個文件夾,命名服務器和客戶端,服務器文件夾有快遞和API邏輯,而客戶端文件夾只是一個反應的應用程序。

+0

你爲什麼在你的快速路線中使用*!你的其他API的名稱是什麼? –

+0

你想做什麼?你想用express來爲js/html服務前端靜態文件嗎? –

回答

0

你需要把路徑正在渲染你的應用程序的HTML文件來

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

這裏是可與反應的快遞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); 
});