Created
December 28, 2016 07:10
-
-
Save zewa666/ca1d5b0cf1fdccd5f756948fa64c66dd to your computer and use it in GitHub Desktop.
Running Aurelia-CLI with Node ExpressJS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
// simple express server | |
var express = require('express'); | |
var path = require('path'); | |
var app = express(); | |
var router = express.Router(); | |
app.use("/scripts", express.static(path.join(__dirname, 'scripts'))); | |
app.get('/', function(req, res) { | |
res.sendfile('./index.html'); | |
}); | |
app.listen(9000); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import gulp from 'gulp'; | |
import browserSync from 'browser-sync' | |
import historyApiFallback from 'connect-history-api-fallback/lib';; | |
import project from '../aurelia.json'; | |
import build from './build'; | |
import {CLIOptions} from 'aurelia-cli'; | |
import * as nodemon from 'gulp-nodemon'; | |
function onChange(path) { | |
console.log(`File Changed: ${path}`); | |
} | |
function reload(done) { | |
browserSync.reload(); | |
done(); | |
} | |
function runNode(cb) { | |
let started = false; | |
return nodemon.default({ | |
script: 'app.js' | |
}).on('start', function () { | |
if (!started) { | |
cb(); | |
started = true; | |
} | |
}); | |
} | |
let serve = gulp.series( | |
build, | |
runNode, | |
done => { | |
browserSync({ | |
online: false, | |
open: false, | |
port: 7000, | |
proxy: "http://localhost:9000", | |
logLevel: 'silent', | |
server: { | |
baseDir: ['.'], | |
middleware: [historyApiFallback(), function(req, res, next) { | |
res.setHeader('Access-Control-Allow-Origin', '*'); | |
next(); | |
}] | |
} | |
}, function (err, bs) { | |
let urls = bs.options.get('urls').toJS(); | |
console.log(`Application Available At: ${urls.local}`); | |
console.log(`BrowserSync Available At: ${urls.ui}`); | |
done(); | |
}); | |
} | |
); | |
let refresh = gulp.series( | |
build, | |
reload | |
); | |
let watch = function() { | |
gulp.watch(project.transpiler.source, refresh).on('change', onChange); | |
gulp.watch(project.markupProcessor.source, refresh).on('change', onChange); | |
gulp.watch(project.cssProcessor.source, refresh).on('change', onChange) | |
} | |
let run; | |
if (CLIOptions.hasFlag('watch')) { | |
run = gulp.series( | |
serve, | |
watch | |
); | |
} else { | |
run = serve; | |
} | |
export default run; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
npm install --save express | |
npm install --save-dev nodemon |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Cool, I was exactly trying the same approach with 'proxy' option, except I wasn't able to figure out the "runNode" task.
Thanks Vildan.