【发布时间】:2019-11-12 13:24:25
【问题描述】:
我目前有一个以 x url 形式托管的 ionic webapp(例如 www.myApp.com)。我的应用程序有子文件夹,例如myApp.com/home 或 myApp.com/details。不幸的是,每当我刷新这些子文件夹中的任何一个时,都会收到类似“Cannot GET /home”之类的错误(只有基本 URL/域有效)。
如果可能,是否有任何方法可以在 Google Domains 上托管这些子文件夹(因此如果刷新这些链接,GD 会自动将这些链接重定向回 baes myApp.com 链接)或者我需要在其中做些什么代码本身来完成这个?
编辑:我现在明白我必须使用我的网络服务器重新路由,以便当用户在错误链接上刷新页面时我可以返回 index.html,例如(ionicSite.com/Page)。作为参考,我在下面包含了我的 server.js 文件:
var express = require('express');
var app = express(); // create our app w/ express
var morgan = require('morgan'); // log requests to the console (express4)
var bodyParser = require('body-parser'); // pull information from HTML POST (express4)
var cors = require('cors');
// Server Redirection changes
const targetURL = "www.ambitia.org";
const path = require ('path');
app.use(morgan('dev')); // log every request to the console
app.use(bodyParser.urlencoded({'extended':'true'})); // parse application/x-www-form-urlencoded
app.use(bodyParser.json()); // parse application/json
app.use(bodyParser.json({ type: 'application/vnd.api+json' })); // parse application/vnd.api+json as json
// app.use(methodOverride());
app.use(cors());
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header('Access-Control-Allow-Methods', 'DELETE, PUT');
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.use(express.static('www'));
app.set('port', process.env.PORT || 5000);
app.get('/home', (req,res) => {
console.log('You asked for my home!');
res.sendFile(path.resolve('/main-es2015.js'));
})
/**
app.get('/*', (req, res) => {
('Attempting the redirection goal');
res.sendFile(path.resolve(__dirname));
}) **/
app.listen(app.get('port'), function () {
console.log('Express server listening on port ' + app.get('port'));
});
【问题讨论】:
-
“我的应用有子文件夹”是什么意思?这是来自 Angular/Ionic 应用程序的 /home 或 /details 路由吗?还是服务器中真正的“子文件夹”?
-
抱歉,应该澄清一下。正如你所说,它基本上只是路线,例如/home 或 /details 来自 Ionic 应用程序本身。只是当我刷新时,例如/home,它会导致错误(因为域仅指向基本 url)
-
为了清楚起见,我觉得这个问题总结我自己比较好stackoverflow.com/questions/35117884/…
标签: angular ionic-framework browser web-development-server