Я использую connect-history-api-fallback в сочетании со страницей . js router .
page('/', index);
page('/about', about);
page();
function index() {
console.log("viewing index");
}
function about() {
console.log("viewing about");
}
Маршрутизация работает нормально, но когда я пытаюсь получить доступ к API, маршрутизация предотвращает вызов.
GET localhost:4000/ # index view function
GET localhost:4000/about # about view function
GET localhost:4000/api/todos # Nothing, instead of JSON
Вот сервер ...
const express = require("express");
const history = require('connect-history-api-fallback');
var todos = require("./api/routes/todos");
var app = express();
// Allow 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 listening at http://%s:%s", host, port);
});