MEAN - 执行server.js的默认gulp文件

时间:2016-02-03 21:20:24

标签: javascript node.js express gulp

我最近开始重构我的gulp服务任务,因为我开始构建我的应用程序的后端。这是我的(相关)文件夹结构:

projectName --gulp ----tasks --public ---dist --src ---server -----server.js

问题是,为什么我的server.js文件是从我的默认gulp文件执行的?

ernestscomputer:musicomp Ernesto_S$ gulp
[15:47:44] Using gulpfile ~/Desktop/PROJECTS/musicomp/gulpfile.js
livereload[tiny-lr] listening on 35729 ...
[15:47:44] Starting 'clean'...
[15:47:44] Finished 'clean' after 5.54 ms
[15:47:44] Starting 'default'...
[15:47:44] Starting 'bundle-lib'...
[15:47:44] Starting 'bundle-app'...
[15:47:44] Starting 'compileSass'...
[15:47:44] Starting 'pipeMaterialCss'...
[15:47:44] Finished 'default' after 46 ms
Musicomp is up and running...
[15:47:44] Finished 'compileSass' after 170 ms
[15:47:44] Finished 'pipeMaterialCss' after 167 ms
[15:47:45] Finished 'bundle-app' after 491 ms
[15:47:45] Finished 'bundle-lib' after 729 ms
[15:47:45] Starting 'build'...
[15:47:45] Finished 'build' after 167 ms

我的server.js文件:

"use strict";

    const express = require('express'),
        Router = require('./router'),
        bodyParser = require('body-parser'),        
        //dbConfig = require('./config/dbConfig'),
        //mongoose = require('mongoose'),
        server = express(),
        port = process.env.PORT || 9000;

    new Router(express, server);
    //server.use(express.static('public/dist')); now in Router
    server.listen(port);

    server.get('/', function(req, res) {
            res.sendfile('./public/dist/index.html'); // load our public/index.html file
        });

    //mongoose.Promise = Promise;
    //mongoose.connect(dbConfig.databaseUrl);

    console.log('Musicomp is up and running...');

我的gulp文件:

var gulp = require('gulp');

    gulp.task('default', ['clean'], function () {
        gulp.start('build');
     });

gulp.task('build', ['bundle-lib', 'bundle-app', 'compileSass', 'pipeMaterialCss'], function () {
    return gulp.src(['src/**/*.html', 'src/img/**'])
    .pipe(gulp.dest('public/dist')); 
});

var gulp = require('gulp'),
    concat = require('gulp-concat'),
    uglify = require('gulp-uglify'),
    sourcemaps = require('gulp-sourcemaps'),
    gulpUtil = require('gulp-util'),
    liveReload = require('gulp-livereload');

    gulp.task('bundle-app', function() {
        return gulp.src(['src/app.js', 'src/**/*.js' ,'!src/server/**/*.*',])
            .pipe(sourcemaps.init())
                .pipe(concat('app.min.js'))
                //.pipe(gulp.dest('./tmp/dist'))
                //.pipe(uglify().on('error', gulpUtil.log))
            .pipe(sourcemaps.write('./'))
            .pipe(gulp.dest('./public/dist'))
            .pipe(liveReload());
    });

    gulp.task('bundle-lib', function() {
        return gulp.src(['node_modules/angular/angular.min.js', 
                        'node_modules/angular-route/angular-route.min.js', 
                        'node_modules/angular-aria/angular-aria.js',
                        'node_modules/angular-animate/angular-animate.js',
                        'node_modules/angular-material/angular-material.js',
                        'node_modules/angular-messages/angular-messages.min.js',
                        'node_modules/lodash/dist/lodash.js'
            ])
            .pipe(sourcemaps.init())
                .pipe(concat('lib.min.js'))
            .pipe(sourcemaps.write('./'))   
            .pipe(gulp.dest('./public/dist'));
    });

var gulp = require('gulp'),
    sass = require('gulp-sass'),
    maps = require('gulp-sourcemaps'),
    liveReload = require('gulp-livereload');


gulp.task('compileSass', function () {
    return gulp.src("src/scss/main.scss")
        .pipe(maps.init())
        .pipe(sass())
        .pipe(maps.write('./'))
        .pipe(gulp.dest('./public/dist'))
        .pipe(liveReload());    
}); 

gulp.task('pipeMaterialCss', function () {
    return gulp.src(['node_modules/angular-material/angular-material.min.css'])
    .pipe(gulp.dest('./public/dist')); 
});

1 个答案:

答案 0 :(得分:0)

抱歉,但我没有提供问题的关键...

var gulp = require('gulp');

    gulp.task('serve', ['watchFiles'])

        var liveServer = require("gulp-live-server"),
            serverPath = "./src/server/server.js",
            server = liveServer([serverPath]);

        server.start();

是的,我编辑了我的gulp任务,没有添加功能......最终答案

var gulp = require('gulp');

    gulp.task('serve', ['watchFiles'], function() {

        var liveServer = require("gulp-live-server"),
            serverPath = "./src/server/server.js",
            server = liveServer([serverPath]);

        server.start(); 
    });