forked from ryanj/http-base
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
47 lines (41 loc) · 1.22 KB
/
server.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
var cc = require('config-multipaas'),
fs = require('fs'),
http = require("http"),
st = require("st"),
Router = require("routes-router"),
sendJson = require("send-data/json"),
sendHtml = require("send-data/html"),
sendError= require("send-data/error")
var config = cc()
var app = Router()
// Routes
app.addRoute("/status", function (req, res, opts, cb) {
sendJson(req, res, "{status: 'ok'}\n")
})
app.addRoute("/", function (req, res, opts, cb) {
var data = fs.readFileSync(__dirname + '/index.html');
sendHtml(req, res, {
body: data.toString(),
statusCode: 200,
headers: {}
})
})
app.addRoute("/hostname", function (req, res, opts, cb) {
var data = "<p>Hostname: " + config.get('HOSTNAME') + "</p>\n";
console.log("Got hostname");
sendHtml(req, res, {
body: data,
statusCode: 200,
headers: {}
})
})
// Serve all the static assets prefixed at /static
// so GET /js/app.js will work.
app.addRoute("/*", st({
path: __dirname + "/static",
url: "/"
}))
var server = http.createServer(app)
server.listen(config.get('PORT'), config.get('IP'), function () {
console.log( "Listening on " + config.get('IP') + ", port " + config.get('PORT') )
});