Javascript 连接历史api回退防止api调用
我使用的是与 路由工作正常,但当我尝试访问API时,路由会阻止调用Javascript 连接历史api回退防止api调用,javascript,express,routing,single-page-application,Javascript,Express,Routing,Single Page Application,我使用的是与 路由工作正常,但当我尝试访问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 his
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);
});
这使它起作用
app.use(history({
rewrites: [
{
from: /^\/api\/.*$/,
to: function(context) {
return context.parsedUrl.path
}
}
]
}))
这使它起作用
app.use(history({
rewrites: [
{
from: /^\/api\/.*$/,
to: function(context) {
return context.parsedUrl.path
}
}
]
}))