簡體   English   中英

捕獲加載空白頁面 Angular NodeJs 的所有路由

[英]Catch all routes loading blank page Angular NodeJs

我正在使用路由(NodeJs 作為后端)開發 Angular 應用程序。 通過頁面路由工作正常,但是當我嘗試重新加載頁面時,它給出“無法加載路徑/home”。 我瀏覽並找到了幾個像THIS 的解決方案。

我的代碼是

app.get('*', function (req, res) {
        res.sendFile('mypath/index.html');
    });

但這會加載空白頁面。 有什么辦法可以解決這個問題嗎?

這就是我的 server.js 的樣子

var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var methodOverride = require('method-override');

var port = process.env.PORT || 8080; // set our port
var staticdir = process.env.NODE_ENV === 'production' ? 'dist.prod' : 'dist.dev'; // get static files dir

// get all data/stuff of the body (POST) parameters
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(bodyParser.urlencoded({ extended: true })); // parse application/x-www-form-urlencoded

app.use(methodOverride('X-HTTP-Method-Override')); // override with the X-HTTP-Method-Override header in the request. simulate DELETE/PUT
app.use(express.static(__dirname + '/' + staticdir)); // set the static files location /public/img will be /img for users
// routes ==================================================
app.set('views', __dirname + '/app');
app.set('view engine', 'html');
require('./devServer/routes')(app); // configure our routes

// start app ===============================================
app.listen(port);                   // startup our app at http://localhost:8080
console.log('Starting sever on port ' + port);       // shoutout to the user
exports = module.exports = app;             // expose app
app.get('/', function (req, res) {
        res.render('index', {
page: 'index'
}
    });

然后將其輸出到您的配置中,

app.set('views',  'directory where index.html is');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM