-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.js
51 lines (40 loc) · 1.08 KB
/
http.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
"use strict";
/* eslint no-unused-vars: 0 */
var PORT = process.env.PORT || 3000,
AUTH_TOKEN = process.env.AUTH_TOKEN || "DEFAULT_AUTH_TOKEN";
var app = require("express")();
var bodyParser = require("body-parser");
// parse POST bodies
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// content-type
app.use(function(req, res, next) {
res.header("Content-Type", "application/json");
next();
});
// auth
app.use(function(req, res, next) {
var header = req.header("X-Auth-Token");
if (header && header === AUTH_TOKEN) { return next(); }
return res.status(401).json({ error: "unauthorized" });
});
function listen() {
// 404
app.use(function(req, res, next) {
res.status(404).json({ error: "not found" });
});
// error handler
app.use(function(err, req, res, next) {
res.status(500).json({ error: err });
});
// start
app.listen(PORT, function() {
console.log("http server listening on port", PORT);
});
}
module.exports = {
get: app.get.bind(app),
put: app.put.bind(app),
post: app.post.bind(app),
listen: listen
};