Reputation: 6482
I'm trying to get my express app to always return 'index.html' which is the layout file that contains my angular app scaffold.
In my angular app when i'm at route "/home" I want to be able to refresh the page, so to do that I need my router to return me index.html.
For my html routing i'm using the following - All of my templates (home.html) are stored in the /templates directory:
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);
});
This is obviously affecting my REST API requests, in that they're not sending back index.html.
For example, I have an /islands api route which returns back 'Island' objects from my mongo db.
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);
});
});
This is now returning me index.html.
Here's how i'm using the controllers above incase that helps.
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"));
I'm a little confused as to how I can get index.html only return for html requests, not requests made to my api.
Upvotes: 2
Views: 1399
Reputation: 715
Check order of your routes, put the api routes before your sendfile ones.
Upvotes: 4