【问题标题】:How to set a base URL for NodeJs applications using ExpressJs server and AngularJs for frontend?如何使用 ExpressJs 服务器和 AngularJs 为前端设置 NodeJs 应用程序的基本 URL?
【发布时间】:2020-03-31 08:35:31
【问题描述】:

当我在浏览器上加载应用程序时,我想为其添加一个基本 URL。

我的 expressJs 配置是:

App.js

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(expressSanitizer());
app.use(express.static(__dirname + '/webapp'));
app.engine('html',require('ejs').renderFile);
app.set('view engine', 'html');

AngularJs 路由器配置:

index.js

$urlRouterProvider
        .when('','login')
        .when('/','login')
        .otherwise(function($injector, $location){
            var state= $injector.get('$state');
            state.go('error', { url: $location.path() }, { location: true });
        });

        //define states
        $stateProvider
        .state('/',{
            templateUrl: 'public/login.html',
            controller: 'login',
            authenticated:false
        })
        .state('login',{
            url: '/login',
            templateUrl: 'public/login.html',
            authenticated:false
            // controller: 'login'
            // template: '<h1> Routing to login page </h1>'
        })

我希望它类似于我们在 Apache Tomcat 中部署应用程序时的情况。

目前,当我启动服务器时,url 类似于localhost:8080/login,但我希望它为localhost:8080/CRM/login

【问题讨论】:

    标签: node.js angularjs express angular-ui-router


    【解决方案1】:

    所以,我就这样想出了解决办法。

    我在 index.html 中添加了一个基本标记,并在我的 App.js 中的所有 api 中添加了“/CRM”

    index.html

    <base href="/CRM/">
    

    App.js

    app.use('/CRM/users',appRoutes);
    

    【讨论】:

      猜你喜欢
      • 2021-08-18
      • 2019-12-22
      • 1970-01-01
      • 2016-08-15
      • 2011-05-21
      • 2022-01-20
      • 2017-01-04
      • 2019-09-06
      • 1970-01-01
      相关资源
      最近更新 更多