使用Gulp和Glue构建HaPi应用程序

时间:2016-06-01 16:02:19

标签: javascript node.js gulp babeljs hapijs

我正在努力建立我的Hapi应用程序,它使用Glue manifest配置服务器。

gulp console

我也使用Babel将我的代码转发到dist目录。

我的目录结构如下:

的src / index.js

"use strict";

import fs from 'fs';
import Glue from 'glue';
import minimist from 'minimist';

const args = minimist(process.argv.slice(2));

// load configuration
const envConfigFile = args['env-config'] || 'config/local.json';
const envConfig = JSON.parse(fs.readFileSync(envConfigFile));
const manifest = envConfig.manifest;
manifest.server.app = envConfig;

// compose Hapi server from config file
Glue.compose(manifest, { relativeTo: process.cwd() }, (err, server) => {
  if (err) { return console.error(err); }
  server.start(() => {
    console.log('hapi days!');
  });
});

gulpfile.js

const gulp = require("gulp");
const connect = require('gulp-connect'); // Runs a local dev server
const babel = require("gulp-babel"); // Use Babel with gulp
const lint = require('gulp-eslint'); // Lint JS files

const config = {
    port: 9005,
    devBaseUrl: 'http:localhost',
    paths: {
        js: 'src/**/*.js',
        dist: 'dist/',
        mainJs: 'dist/index.js'
    }
};

// Start a local development server
gulp.task('connect', function() {
    connect.server({
        root: ['dist'],
        port: config.port,
        base: config.devBaseUrl,
        livereload: true
    });
});

gulp.task("babel", function() {
    return gulp.src(config.paths.js)
        .pipe(babel())
        .pipe(gulp.dest(config.paths.dist));
});

gulp.task("babel-watch", function() {
    gulp.watch(config.paths.js, ["babel"]);
});

gulp.task("lint", function () {
    return gulp.src(config.paths.js)
        .pipe(lint({config: 'eslint.config.json'}))
        .pipe(lint.format());
});

gulp.task('watch', function () {
    gulp.watch(config.paths.js, ['lint']);
});

gulp.task("default", ["babel", "babel-watch", "lint", "watch"]);

配置/ config.json

{
  "sql": {
    "client": "mysql",
    "connection": {
      "host": "localhost",
      "user": "root",
      "password": "",
      "database": "apptest"
    }
  },
  "manifest": {
    "server": {

    },
    "connections": [
      {
        "port": 8000,
        "labels": ["web"]
      },
      {
        "port": 8001,
        "labels": ["admin"]
      }
    ],
    "registrations": [
      {
        "plugin": {
          "register": "hapi-knex-builder",
          "options": {}
        }
      },
      {
        "plugin": {
          "register": "good",
          "options": {
            "reporters": {
              "console": [
                {
                  "module": "good-console"
                },
                "stdout"
              ]
            }
          }
        }
      }
    ]
  }
}

当我使用该节点运行我的应用程序时,它工作正常,但不会在浏览器上启动(假设原因是缺少路由)。

我使用node来运行我的代码:

node dist/ --env-config config/config.json

输出:

node console

0 个答案:

没有答案