From 1438aea177c7e7defae97b48621bba2c88166acb Mon Sep 17 00:00:00 2001 From: Kylart Date: Mon, 19 Jun 2017 01:12:38 +0200 Subject: [PATCH] Added serverStart to enable hot reloading on server. --- README.md | 8 +++++++- assets/serverStart.js | 44 +++++++++++++++++++++++++++++++++++++++++++ package.json | 4 +++- 3 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 assets/serverStart.js diff --git a/README.md b/README.md index c2c6121..78e3115 100755 --- a/README.md +++ b/README.md @@ -66,11 +66,17 @@ npm run bstart ## Run in dev -##### Start +##### Start all app ``` npm run dev ``` +#### Start only server with hot reloading +_using this with a REST api client is kinda useful_. +``` +npm run dev:server +``` + ##### Lint ``` npm run lint diff --git a/assets/serverStart.js b/assets/serverStart.js new file mode 100644 index 0000000..d82bcc0 --- /dev/null +++ b/assets/serverStart.js @@ -0,0 +1,44 @@ +/** + * Created by Kylart on 19/06/2017. + */ + +'use strict' + +process.NODE_ENV = 'development' + +const {join} = require('path') +const Nuxt = require('nuxt') +const http = require('http') +const chokidar = require('chokidar') + +let nuxt = null +let server = null + +const initServer = async () => { + const initFile = require(join(__dirname, '..', 'assets', 'api', 'main.js')) + + /** + * Nuxt config + */ + const config = require('../nuxt.config.js') + + /** + * Server config + */ + nuxt = new Nuxt(config) + const route = initFile.route(nuxt) + server = http.createServer(route) + + await nuxt.build() + server.listen(3000) +} + +initServer() + +chokidar.watch(join(__dirname), {ignored: './menu.js'}).on('all', (event, path) => { + console.info(`${path}: ${event}`) + + server.close() + + initServer().catch((err) => { void err }) +}) diff --git a/package.json b/package.json index d23ddca..2d17d90 100755 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "license": "MIT", "scripts": { "dev": "cross-env NODE_ENV=development electron .", + "dev:server": "node assets/serverStart.js", "cloc": "cloc $(git ls-files | grep -vE \".mkv|mp4|lock\")", "test:server": "nyc ava --verbose --serial test/server.test.js", "test:front": "nightwatch", @@ -101,7 +102,8 @@ "nightwatch.conf.js", "**/*.test.js", "**/node_modules/**", - "**/openExternal.js" + "**/openExternal.js", + "assets/serverStart.js" ], "watermarks": { "lines": [