我想解决一个困扰我一段时间的问题。
设想情况:
问题是当我使用Gulp启动本地服务器w/ Live Reload时。它很好地启动了我的角度应用程序,但是当我修改文件时,Live (页面刷新)破坏了我的应用程序,给了我一个“无法获取”,因为我的server.js (节点服务器)文件有一种特殊的方式将用户重定向到我的"index.html“文件。之所以这样做,是因为我在角应用程序中启用了"HTML5模式“(对于漂亮的URL),并且我必须在server.js文件中指定它才能正常工作。
在我打开我的角度应用程序中的"html5“之前,一切都很好,但是由于特殊的重定向,我的Gulp.js文件没有被正确地编写出来。
但是,如果我运行"node server.js“,这个角度的应用程序就会像预期的那样工作,并获得页面刷新,在开发过程中遇到这个问题会让我很恼火。
问题:
(我漏掉了几个文件夹和文件,这样就更容易了)
档案结构:
根
Server.js (节点服务器)
var express = require('express');
var app = express();
app.use('/js', express.static(__dirname + '/front/js'));
app.use('/build', express.static(__dirname + '/../build'));
app.use('/css', express.static(__dirname + '/front/css'));
app.use('/images', express.static(__dirname + '/front/images'));
app.use('/pages', express.static(__dirname + '/front/pages'));
app.all('/*', function(req, res, next) {
// Sends the index.html for other files to support HTML5Mode
res.sendFile('/front/index.html', { root: __dirname });
});
var port = process.env.PORT || 8000;
app.listen(port);
Gulpfile (“连接”从第67行开始)
var gulp = require ('gulp'),
sync = require ('browser-sync'),
bower = require ('gulp-bower'),
htmlify = require ('gulp-minify-html'),
uglify = require ('gulp-uglify'),
prefix = require ('gulp-autoprefixer'),
minify = require ('gulp-minify-css'),
imgmin = require ('gulp-imagemin'),
rename = require ('gulp-rename'),
concat = require ('gulp-concat'),
inject = require ('gulp-inject'),
connect = require ('gulp-connect'),
open = require ('gulp-open');
// Bower Task
gulp.task('bower', function() {
return bower()
.pipe(gulp.dest('front/js/vendors'));
});
// HTML Task
gulp.task('html', function(){
gulp.src('front/**/*.html')
.on('error', console.error.bind(console))
.pipe(gulp.dest('build'))
.pipe(connect.reload());
});
// Styles Task (Uglifies)
gulp.task('styles', function(){
gulp.src('front/**/*.css')
.pipe(minify())
.on('error', console.error.bind(console))
.pipe(gulp.dest('build'))
.pipe(connect.reload());
});
// Scripts Task (Uglifies)
gulp.task('scripts', function(){
gulp.src('front/**/*.js')
.pipe(uglify())
.on('error', console.error.bind(console))
.pipe(gulp.dest('build'))
.pipe(connect.reload());
});
// Image Task (compress)
gulp.task('images', function(){
gulp.src('front/images/*')
.pipe(imgmin())
.pipe(gulp.dest('build/images'))
.pipe(connect.reload());
});
// Connect Task (connect to server and live reload)
gulp.task('connect', function(){
connect.server({
root: 'front',
livereload: true
});
});
// Watch Task (watches for changes)
gulp.task('watch', function(){
gulp.watch('front/*.html', ['html']);
gulp.watch('front/js/*.js', ['scripts']);
gulp.watch('front/css/*.css', ['styles']);
gulp.watch('front/images/*', ['images']);
});
// Open Task (starts app automatically)
gulp.task("open", function(){
var options = {
url: "http://localhost:8080",
app: "Chrome"
};
gulp.src("front/index.html")
.pipe(open("", options));
});
gulp.task('default', ['html', 'styles', 'scripts', 'images', 'connect', 'watch', 'open']);
角应用程序
// App Starts
angular
.module('app', [
'ui.router',
'ngAnimate',
'angular-carousel'
])
.config(['$urlRouterProvider', '$stateProvider', '$locationProvider', function($urlRouterProvider, $stateProvider, $locationProvider) {
$urlRouterProvider.otherwise("/");
$stateProvider
// Home Page
.state('home', {
url: '/',
templateUrl: 'pages/home.html',
controller: 'homeCtrl'
})
// use the HTML5 History API
$locationProvider.html5Mode(true);
}])
Index.html
<!DOCTYPE html>
<html class="no-js" ng-app="app" ng-controller="mainCtrl">
<head>
<title>Website</title>
<meta charset="utf-8"/>
<!-- Universal Stylesheets -->
<link href="css/stylesheet.css" type="text/css" rel="stylesheet" />
<!-- Sets base for HTML5 mode -->
<base href="/"></base>
</head>
<body>
<section class="row main" ui-view></section>
<!-- Javascript -->
<script src="js/scripts.js"></script>
</body>
</html>
提前感谢您的帮助!
发布于 2014-11-01 17:45:55
以下是一个相当好的解决方案:)
下面是您应该使用的内容:
gulp.task('connect', connect.server({
root: ['build'],
port: 9000,
livereload: true,
open: {
browser: 'Google Chrome'
},
middleware: function(connect, opt) {
return [ historyApiFallback ];
}
}));
这是我用于SPA开发的模块:
"connect-history-api-fallback": "0.0.5",
另外,AngularJs有一个避免设置基href的简洁解决方案。
$locationProvider.html5Mode({enabled: true, requireBase: false})
https://stackoverflow.com/questions/26108532
复制相似问题