This repository has been archived by the owner on Feb 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample.js
87 lines (80 loc) · 2.43 KB
/
example.js
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// # Pult
//
// ## Client Library Example
//
// This is an example of a Node HTTP server using Pult to bind to a local
// `.dev` domain.
//
var http = require('http');
var pult = require('pult');
// The server will be available at `http://hello.dev`.
var SERVER_NAME = 'hello';
// The port to fall back to if Pult is not available.
var DEFAULT_PORT = 8080;
// Create a simple HTTP server that responds with "Hello, World!" to every
// request.
var server = http.createServer(function(req, res) {
var body = 'Hello, World!';
res.writeHead(200, {
'Content-Type': 'text/plain',
'Content-Length': body.length
});
res.write(body);
res.end();
});
// Only try to use Pult in development.
if (process.env.NODE_ENV == 'development') {
// Check if Pult is running.
pult.serverStatus(function(err, up) {
if (err) throw err;
// If the Pult server is up, retrieve a port number for `SERVER_NAME`
// from it.
if (up) {
pult.getPort(SERVER_NAME, function(err, port, domain) {
if (err) throw err;
// Bind the HTTP server to the assigned port.
server.listen(port);
// Output a link to the `.dev` domain.
console.log('http://' + domain);
});
// If the Pult server is not running, bind the HTTP server to the `PORT`
// environment variable or the default development port.
} else {
var port = process.env.PORT || DEFAULT_PORT;
server.listen(port);
// Output a link to the server.
console.log('http://localhost:' + port);
}
});
// In production, bind the HTTP server to the `PORT` environment variable.
} else {
server.listen(process.env.PORT);
}
// ### Starting the Pult server automatically
//
// Though not recommended because some developers may not want to run the Pult
// server, it is possible to automatically start it before your server starts.
//
// Callback to start the server once Pult is running.
function startServer() {
pult.getPort(SERVER_NAME, function(err, port, domain) {
if (err) throw err;
server.listen(port);
console.log('http://' + domain);
});
}
// Check if the Pult server is already running.
pult.serverStatus(function(err, up) {
if (err) throw err;
// If Pult is already running, start the server.
if (up) {
startServer();
// Otherwise, spawn the Pult server.
} else {
pult.spawnServer(function(err) {
if (err) throw err;
// Then start the HTTP server.
startServer();
});
}
});