簡體   English   中英

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

[英]how to resolve ExpressJS routing when using AngularJS html5Mode (back4app/Parse-Server)

我正在使用使用Parse-Server的back4app BaaS服務。 對於ClientSide,我使用html5Mode(true)運行AngularJS;

我的問題是這不起作用: 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>

這是我的app結構:

在此輸入圖像描述

您可以通過在app.js和root html文件中進行少量更改來使其工作

我假設你已經定義了$locationProvider.html5Mode(true); 你定義路線的地方。 然后在索引html中定義base href

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

這個答案可能有助於配置您的服務器

由於您使用的是Cloud Code,因此cloud / app.js上的文件最后一行不應該有app.listen()。 你能試試嗎?

我遇到了同樣的問題並做了以下事情

我已將此路由作為最后一個選項,因此當快速路由器用盡選項時,它將呈現索引文件,其中是角度應用程序。 Angular內部路由器將解析該路由並繪制視圖。

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

顯然你可以根據自己的需要編寫一個更聰明的正則表達式而不是* ,但你明白了。

暫無
暫無

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

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