Express + Angular,总是发送html请求的索引文件,而不是api请求

我试图让我的快递应用程序始终返回包含我的angular应用脚手架的布局文件“index.html”。

在我的angular应用程序,当我在路由“/家”我想能够刷新页面,所以要做到这一点,我需要我的路由器返回我index.html。

对于我的HTML路由我使用以下 – 所有我的模板(home.html)存储在/ templates目录中:

var express = require("express"), router = express.Router(), path = require("path"), root = path.join(__dirname, "../../"); router.use(express.static(__dirname + "/../../templates")); router.get("*", function(req, res) { //Response options var options = { root: root, }; res.sendFile("layouts/index.html", options); }); 

这显然影响了我的REST API请求,因为他们没有发回index.html。

例如,我有一个/ islands api路由,从我的mongo db返回“Island”对象。

 var router = require("express").Router(), Island = require("../../model/island"); router.get("/", function (req, res, next) { Island.findOne({user_id: req.user.email}, function (error, island) { if (error) { return next(error); } return res.json(island); }); }); 

这现在返回给我index.html。

以下是我如何使用上面的incase控制器帮助。

 app.use(require("./controllers/api/static")); //Returning static assets app.use(require("./controllers/api/views")); //This is where the get route for returning .index.html is stored app.use("/api/island",require("./controllers/api/island")); 

我有点困惑,我如何才能得到index.html只返回的HTML请求,而不是对我的API的要求。

检查你的路由顺序,把你的sendfile之前的api路由。