如何在使用AngularJS html5Mode(back4app / Parse-Server)时解析ExpressJS路由

时间:2017-01-31 05:05:44

标签: javascript angularjs express parse-server back4app

我使用的是使用Parse-Server的back4app BaaS服务。对于ClientSide,我使用html5Mode运行AngularJS(true);

我的问题是这不起作用:http://app.rizop.tv/dashboard 虽然这是正常的:http://app.rizop.tv

知道如何修复expressJS以正确的方式处理我的路线吗?

我有这个配置:

云\ app.js

// Helper modules that will be used
var path = require('path');
var bodyParser = require('body-parser')

// This imports the Router that uses the template engine
var index = require('./routers/index');

// Sets the template engine as EJS
app.set('view engine', 'ejs');

// This defines that the 'views' folder contains the templates
app.set('views', path.join(__dirname, '/views'));

// These options are necessary to 
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())

// This bind the Router to the / route
app.use('/', index)

// Starts listening in the routes
app.listen();

云\路由器\ index.js

// Importing express
var express = require('express');

// Creating a Router
var route = express.Router();

// Defining a route that binds the GET method
route.get('/', function(req, res) {
  // This is the code that renders the template
  res.render('index', {testParam: 'Back4Apper'});
});

module.exports = route;

云\视图\ index.ejs

<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  ...
</body>
...
</body>
</html>

这是我的应用结构:

enter image description here

3 个答案:

答案 0 :(得分:2)

您可以通过在app.js和root html文件中进行少量更改来使其工作

我假设您已定义了$locationProvider.html5Mode(true);您定义路线的位置。然后在索引html中定义基本href

<head>
  <base href="/">
  ...
</head>

This answer可能有助于配置您的服务器

答案 1 :(得分:1)

由于您使用的是Cloud Code,因此cloud / app.js上的文件最后一行不应包含app.listen()。你能试试吗?

答案 2 :(得分:0)

我遇到了同样的问题并执行了以下操作

我已将此路由作为最后一个选项,因此当快速路由器用完选项时,它将呈现索引文件,其中是角度应用程序。 Angular内部路由器将解析该路由并绘制视图。

router.get('*', function (req, res) {
    res.render('index', {testParam: 'Back4Apper'});
});

显然,你可以根据自己的需要编写一个更聪明的正则表达式而不是*但是你明白了。