捕获所有加载空白页Angular NodeJs

时间:2016-03-21 12:12:15

标签: angularjs node.js

我正在研究带路由的Angular应用程序(NodeJs作为后端)。通过页面进行路由工作正常,但是当我尝试重新加载页面时,它会“无法加载路径/主页”。我浏览了一下,得到了一些像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

1 个答案:

答案 0 :(得分:1)

app.get('/', function (req, res) {
        res.render('index', {
page: 'index'
}
    });

然后将其输入您的配置

app.set('views',  'directory where index.html is');
相关问题