启动的Node.js服务器瓦特/咕嘟咕嘟"连接"直播重装(角应用W / HTML5模式)休息 [英] Starting Node.js server w/ Gulp "connect" breaks on live reload (Angular app w/ html5 mode)
问题描述
我试图解决一直缠着我一段时间的一个问题。
I'm trying to solve a problem that's been bugging me for awhile.
问题是,当我使用咕嘟咕嘟启动我的本地服务器瓦特/现场刷新。它开始了我的角应用程序就好了,但是当我做一个文件更改住刷新(页面刷新)打破我的应用程序,并给了我一个CAN NOT GET,因为我的server.js(节点服务器)文件有它重定向一个特殊的方式用户在我的的index.html文件。这样做的原因是因为我有HTML5模式的角度应用使能(为pretty的URL)和我在server.js文件中指定该为它才能正常工作。
The problem is when I use Gulp to start up my local server w/ Live Reload. It starts up my Angular app just fine, but when I make a file change Live Reload (page refreshes) breaks my app and gives me a "CANNOT GET" because my server.js (node server) file has a special way that it redirects the user to my "index.html" file. The reason for this is because I have "HTML5 mode" enabled in the Angular app (for pretty URL's) and I have to specify this in the server.js file for it to properly work.
在我转身HTML5的我里面的应用程序的角度,一切运行良好,但特殊的,因为我的重定向文件Gulp.js不正确写入意识到这一点。
Before I turned "html5 on" inside my angular app everything worked fine, but because of the special redirecting my Gulp.js file isn't written correctly to be aware of this.
如果我运行节点server.js不过,棱角分明的应用程序运行正常,并得到刷新页面,它只是刺激有当我开发这个问题。
If I run "node server.js" however, the angular app works as expected and gets page refreshes, it's just irritating to have this problem when I'm developing.
- 如何写我gulpfile.js正常运行server.js文件?
- 有没有更好的方式来写我的server.js文件?
- 是否可以同时运行多个港口一次(开发模式和生产模式)?
结果
的(我省略了一些文件夹和文件,使其对眼睛更容易)的
ROOT
- 打造的(角生产应用)的
- 前的(角度开发应用程序)的
- gulpfile.js的(任务亚军)的
- server.js的(节点服务器)的
- build (angular production app)
- front (angular development app)
- gulpfile.js (task runner)
- server.js (node server)
结果
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>
在此先感谢您的帮助!
Thanks in advance for your help!
推荐答案
下面是一个相当不错的解决方案:)
Here is a fairly good solution :)
下面是你应该用什么:
gulp.task('connect', connect.server({
root: ['build'],
port: 9000,
livereload: true,
open: {
browser: 'Google Chrome'
},
middleware: function(connect, opt) {
return [ historyApiFallback ];
}
}));
这是我使用的SPA发展模块:
This is the module i use for SPA development:
"connect-history-api-fallback": "0.0.5",
此外AngularJs具有避免设置基地的HREF一个巧妙的解决办法
Also AngularJs has a neat solution for avoiding setting base href
$locationProvider.html5Mode({enabled: true, requireBase: false})
这篇关于启动的Node.js服务器瓦特/咕嘟咕嘟&QUOT;连接&QUOT;直播重装(角应用W / HTML5模式)休息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!