This repository was archived by the owner on Dec 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathindex.js
125 lines (110 loc) · 3.41 KB
/
index.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
(function () {
'use strict';
var request = require("request");
var newrelic = require("newrelic");
var helpers = {};
const winston = require('winston');
const newrelicFormatter = require('@newrelic/winston-enricher');
helpers.logger = winston.createLogger({
level: 'info',
format: winston.format.combine(
winston.format.json(),
// combine with newrelic enricher
newrelicFormatter()
),
defaultMeta: { service: 'front-end' },
transports: [
// just push to console this will be picked up by Newrlic k8s infra agent
new winston.transports.Console()
],
});
/* Public: errorHandler is a middleware that handles your errors
*
* Example:
*
* var app = express();
* app.use(helpers.errorHandler);
* */
helpers.errorHandler = function (err, req, res, next) {
var ret = {
message: err.message,
error: err
};
newrelic.noticeError(err);
res.
status(err.status || 500).
send(ret);
};
helpers.sessionMiddleware = function (err, req, res, next) {
if (!req.cookies.logged_in) {
res.session.customerId = null;
}
};
/* Responds with the given body and status 200 OK */
helpers.respondSuccessBody = function (res, body) {
helpers.respondStatusBody(res, 200, body);
}
/* Public: responds with the given body and status
*
* res - response object to use as output
* statusCode - the HTTP status code to set to the response
* body - (string) the body to yield to the response
*/
helpers.respondStatusBody = function (res, statusCode, body) {
res.writeHeader(statusCode);
res.write(body);
res.end();
}
/* Responds with the given statusCode */
helpers.respondStatus = function (res, statusCode) {
res.writeHeader(statusCode);
res.end();
}
/* Rewrites and redirects any url that doesn't end with a slash. */
helpers.rewriteSlash = function (req, res, next) {
if (req.url.substr(-1) == '/' && req.url.length > 1)
res.redirect(301, req.url.slice(0, -1));
else
next();
}
/* Public: performs an HTTP GET request to the given URL
*
* url - the URL where the external service can be reached out
* res - the response object where the external service's output will be yield
* next - callback to be invoked in case of error. If there actually is an error
* this function will be called, passing the error object as an argument
*
* Examples:
*
* app.get("/users", function(req, res) {
* helpers.simpleHttpRequest("http://api.example.org/users", res, function(err) {
* res.send({ error: err });
* res.end();
* });
* });
*/
helpers.simpleHttpRequest = function (url, res, next) {
request.get(url, function (error, response, body) {
if (error) return next(error);
helpers.respondSuccessBody(res, body);
}.bind({ res: res }));
}
/* TODO: Add documentation */
helpers.getCustomerId = function (req, env) {
// Check if logged in. Get customer Id
var logged_in = req.cookies.logged_in;
// TODO REMOVE THIS, SECURITY RISK
if (env == "development" && req.query.custId != null) {
return req.query.custId;
}
if (!logged_in) {
if (!req.session.id) {
throw new Error("User not logged in.");
}
// Use Session ID instead
return req.session.id;
}
return req.session.customerId;
}
module.exports = helpers;
}());