I have implemented the connect-history-api-fallback along with the page.js router.
page('/', index);
page('/about', about);
page();
function index() {
console.log("viewing index");
}
function about() {
console.log("viewing about");
}
The routing functionality is working correctly, but there seems to be an issue when trying to access the API as the routing interferes with the call.
GET localhost:4000/ # invokes index view function
GET localhost:4000/about # invokes about view function
GET localhost:4000/api/todos # Does not return JSON data as expected
Below is the configuration for the server setup...
const express = require("express");
const history = require('connect-history-api-fallback');
var todos = require("./api/routes/todos");
var app = express();
// Allowing requests from all domains and localhost
app.all("/*", function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header(
"Access-Control-Allow-Headers",
"X-Requested-With, Content-Type, Accept"
);
res.header("Access-Control-Allow-Methods", "GET, POST, HEAD, OPTIONS, PUT, PATCH, DELETE");
next();
});
const root = `${__dirname}/app/dist`
app
.use(history())
.use(express.static(root))
.use(express.json())
.use(todos)
;
var server = app.listen(4000, function () {
var host = server.address().address;
var port = server.address().port;
console.log("App is now listening at http://%s:%s", host, port);
});