-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlivereload.js
More file actions
65 lines (57 loc) · 2.25 KB
/
livereload.js
File metadata and controls
65 lines (57 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*jshint node: true, camelcase: false */
/*global module: true, process: true */
/**
* We create a local webserver for the application, and try to manage the cross-domain problems (with proxy forwarding)
*
* @author Julien Roche
* @version 1.0
* @since 1.0
*/
'use strict';
console.log('Request to start the server for live loading');
//noinspection JSPotentiallyInvalidConstructorUsage
var express = require('express'),
app = express(),
proxy = new require('http-proxy').createProxyServer(),
hostServer = 'localhost',
portServer = 9000,
regExpServerProxy = /^\/api\//;
// ---------------------------------------------------------------------------------------------------------------------
// Add some mime-types
console.log('Define some mime types');
express.static.mime.define(
{
'text/cache-manifest': ['appcache'],
'text/html': ['tmpl'],
'text/less': ['less'],
'image/svg+xml': ['svg'],
'font/opentype': ['otf'],
'application/vnd.ms-fontobject': ['eot'],
'application/octet-stream': ['ttf'],
'application/font-woff': ['woff']
}
);
// ---------------------------------------------------------------------------------------------------------------------
// Redirection on the backend server (avoiding Cross-Domain origins !)
app.all(regExpServerProxy, function (req, res) {
console.log('Call a proxified URL: ' + req.url);
proxy.web(req, res, {
'target': 'http://' + hostServer + ':' + portServer
});
});
// ---------------------------------------------------------------------------------------------------------------------
// Classical configuration
console.log('Initialization of the server (with right configurations and middlewares)');
app.configure(function () {
app.enable('trust proxy');
app.use(express.static(__dirname)); // Our server is the local directory '.'
app.use(express.errorHandler({
dumpExceptions: true,
showStack: true
}));
});
// ---------------------------------------------------------------------------------------------------------------------
// At last
console.log('Server will start now !');
app.listen(process.env.PORT || 9002);
module.exports = app; // Export the current file to the grunt-express task !