在没有 index.html 的 MEAN Stack 应用程序中快速重写干净的 URL
Express Rewriting for Clean URLs in MEAN Stack Application with no index.html
像许多其他干净的 angular URL 重写一样,在像 localhost:3000/profile
这样的干净 URL 上刷新页面时,我得到 GET /profile 404
错误。我一直在尝试使用 Express Rewrite 发送 index.html
文件,但据我所知,我没有要发送的 index.html
,因为它直到 index.js
才呈现] 路线。
我在 app.js
中尝试了以下方法:
app.all('/*', function(req, res, next) {
// Just send the index.html for other files to support HTML5Mode
res.sendFile('index.html', { root: __dirname });
});
并接收 Error: ENOENT: no such file or directory, stat 'C:\xampp\htdocs\healthyu\healthyu\index.html'
我的目录看起来像 this image here, and I can see there is no index.html file in the root directory. I have tried views/index.html
, views/index.ejs
, and views/index
with no luck, and views/index.ejs
actually prompted a download when I refreshed the page.
是否有使用 Express 成功重写 URL 的方法,或者我在 .htaccess
文件中使用 mod-重写会更成功?
所以我需要做的是更改所有 API 请求的服务器请求以使用 /api
前缀,因此 get /api/posts
以减少与 get /posts
这是原始的 API 调用和视图名称以及其他冲突。
我的 index.html
是在我的 express routes/index.js
文件中生成的,所以我只需要确保每当我尝试导航到与主页条目不同的 URL 时我都使用了该文件观点。代码的相关部分如下所示:
var index = require('./routes/index');
var users = require('./routes/users');
app.use('/', index);
app.use('/api', index);
app.use('/users', users);
app.use("/*", index);
像许多其他干净的 angular URL 重写一样,在像 localhost:3000/profile
这样的干净 URL 上刷新页面时,我得到 GET /profile 404
错误。我一直在尝试使用 Express Rewrite 发送 index.html
文件,但据我所知,我没有要发送的 index.html
,因为它直到 index.js
才呈现] 路线。
我在 app.js
中尝试了以下方法:
app.all('/*', function(req, res, next) {
// Just send the index.html for other files to support HTML5Mode
res.sendFile('index.html', { root: __dirname });
});
并接收 Error: ENOENT: no such file or directory, stat 'C:\xampp\htdocs\healthyu\healthyu\index.html'
我的目录看起来像 this image here, and I can see there is no index.html file in the root directory. I have tried views/index.html
, views/index.ejs
, and views/index
with no luck, and views/index.ejs
actually prompted a download when I refreshed the page.
是否有使用 Express 成功重写 URL 的方法,或者我在 .htaccess
文件中使用 mod-重写会更成功?
所以我需要做的是更改所有 API 请求的服务器请求以使用 /api
前缀,因此 get /api/posts
以减少与 get /posts
这是原始的 API 调用和视图名称以及其他冲突。
我的 index.html
是在我的 express routes/index.js
文件中生成的,所以我只需要确保每当我尝试导航到与主页条目不同的 URL 时我都使用了该文件观点。代码的相关部分如下所示:
var index = require('./routes/index');
var users = require('./routes/users');
app.use('/', index);
app.use('/api', index);
app.use('/users', users);
app.use("/*", index);