3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

AngularJSのアプリを/以外の場所に配置する時に気を付けなければならない事

Last updated at Posted at 2014-01-21

普通にAngularJSでアプリを書いていくと、/以下に配置することを前提としたアプリに仕上がると思います。
やらなければならないのは以下の3つです。

  1. .js、.css等を環境ごとのパスに合わせる
  2. リンクとルーティングを環境ごとのパスに合わせる
  3. grunt-connectでテスト出来るようにする

Gruntfile.jsとmain.jsの2か所の変更だけで済むようにテンプレートを更新しました。

progre/template-grunt at static-angularjs

Gruntfile.js
var modRewrite = require('connect-modrewrite');

var projectConfig = {
  root: '/' // ←【変更1】配置先に合わせて変更
};

// connect用の設定
function rewriteMiddleware(connect, options) {
  return [
    modRewrite(['^' + projectConfig.root + '(?!html/).*\\.html$ /index.html [L]']),
    projectConfig.root === '/' ? connect.static(options.base)
      : function(req, res, next) {
        req.url = req.url.replace(new RegExp('^' + projectConfig.root), '/');// ←projectConfig.root以下へのアクセスを/以下へのアクセスに書き換える
        return connect.static(options.base)(req, res, next);
      }
  ];
}
...
    jade: {
      release: {
        options: {
          data: {
            root: projectConfig.root // ←.jadeでroot変数を使えるように
          }
        }
      },
      debug: {
        options: {
          data: {
            root: projectConfig.root, // ←同上
            debug: true
          }
        }
      }
    },
...
index.jade
doctype html
meta(charset='utf-8')
title
link(rel='stylesheet', href='//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css')
link(rel='stylesheet', href='#{root}css/style.css') //←絶対パスはroot変数+パスとして記述する
...
main.ts
...
var root = '/'; // ←【変更2】配置先に合わせて変更

var app = angular.module('app', ['ngRoute', 'ngAnimate']);
app.config(['$routeProvider', '$locationProvider',
    ($routeProvider: ng.route.IRouteProvider, $locationProvider: ng.ILocationProvider) => {
        $locationProvider.html5Mode(true);
        $routeProvider
            .when(root, { // ←↓ root変数+パスとして記述する
                templateUrl: root + 'html/index.html', controller: 'IndexController'
            }).otherwise({
                templateUrl: root + 'html/404.html'
            });
    }
]);
...
3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?