-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
83 lines (76 loc) · 2.27 KB
/
app.js
File metadata and controls
83 lines (76 loc) · 2.27 KB
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
const apm = require('elastic-apm-node').start({
serviceName: 'test-node-app',
serviceVersion: "0.1",
secretToken: '',
serverUrl: 'http://localhost:8200',
transactionSampleRate: 0.5, // Sampling rate to 50%
/*ignoreUrls: [ // Add urls which need not be traced here (those 404 floods perhaps)
'/',
'/ping',
'/sanity',
/^\/admin\//i
],*/
logLevel: "debug", // Possible levels are: trace, debug, info (default), warn, error, and fatal
/* logger: require('pino')({ level: 'info' }, './elastic-apm.log')*/
})
const express = require("express");
const bodyParser = require("body-parser");
const http = require('http');
const app = express();
const ip = "0.0.0.0"
const port = 8080;
app.use(bodyParser.json()); // for parsing application/json
// GET method route
app.get('/get-success', function (req, res) {
query = {"somefield": {"someinnerfield":"somevalue"}}
apm.addTags({"request-url": "/get-success", "query": JSON.stringify(query)});
apm.setCustomContext(query);
apm.setUserContext({
id: 12345,
username: "test-user",
email: "test-user@rapido.bike"
})
res.json({
status: "success",
response: 'GET request to the success api'
})
})
// POST method route
app.post('/post-success', function (req, res) {
res.json({
status: "success",
response: 'POST request to the success api'
})
})
// GET method route
app.get('/get-failure', function (req, res) {
res.status(500).send({
message: 'Route'+req.url+' Not found.'
});
})
// POST method route
app.post('/post-failure', function (req, res) {
/*throw new Error("/post-failure Some random error")*/
/*return res.send("...")*/
res.status(500).send({
status: 'Route'+req.url+' Not found.'
});
})
// 404
// 500 - Any server error
app.use(function(err, req, res, next) {
if (err.status === 404) {
console.error(err.stack);
res.status(404).send({
message: 'Route'+req.url+' Not found.'
});
} else if (err.status == 500) {
console.error(err.stack);
res.status(500).send({
error: err
});
}
});
app.listen(port, ip, function () {
console.log('Express server listening on %d', port);
});