【问题标题】:how to resolve ExpressJS routing when using AngularJS html5Mode (back4app/Parse-Server)使用 AngularJS html5Mode (back4app/Parse-Server) 时如何解析 ExpressJS 路由
【发布时间】:2017-06-16 10:38:09
【问题描述】:

我正在使用使用 Parse-Server 的 back4app BaaS 服务。对于客户端,我使用 html5Mode(true); 运行 AngularJS;

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

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

我有这个配置:

cloud\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;

云\views\index.ejs

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

这是我的应用结构:

【问题讨论】:

    标签: javascript angularjs express parse-server back4app


    【解决方案1】:

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

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

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

    显然,您可以根据需要编写更智能的正则表达式而不是 *,但您明白了。

    【讨论】:

      【解决方案2】:

      cloud/app.js 文件的最后一行不应包含 app.listen(),因为您使用的是 Cloud Code。可以试试吗?

      【讨论】:

        【解决方案3】:

        您可以通过对 app.js 和根 html 文件进行少许更改来使其工作

        我假设您已经在定义路线的地方定义了 $locationProvider.html5Mode(true);。然后在你的 index html 中定义 base href

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

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

        【讨论】:

          猜你喜欢
          • 2013-11-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-02-25
          • 2020-04-04
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多