forked from intel-iot-devkit/intel-iot-examples-datastore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.js
47 lines (38 loc) · 1 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
"use strict";
/* eslint no-unused-vars: 0 */
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, HOST, function() {
console.log("http server at", HOST, "listening on port", PORT);
});
}
module.exports = {
get: app.get.bind(app),
put: app.put.bind(app),
post: app.post.bind(app),
listen: listen
};