-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
56 lines (48 loc) · 1.45 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
const express = require('express')
const log4js = require('log4js')
const config = require('config')
// config.httpServices.integration.enrich.retryInterval = 1;
const MongoClient = require('mongodb').MongoClient;
const app = express()
const logger = log4js.getLogger()
logger.level = config.loglevel
let db
logger.info("Starting echo app!")
logger.debug("Configuration: ", config)
if (!config.persist) {
logger.warn('Persistency is OFF')
} else {
const client = new MongoClient(config.mongodburl, {
useNewUrlParser: true,
useUnifiedTopology: true
})
client.connect((err) => {
if (err) {
logger.fatal("Could not connect to MongoDB!", err)
process.exit(1)
}
logger.warn("Connected successfully to mongodb")
db = client.db(config.dbname)
})
}
const echo = (req, res) => {
// logger.debug("Request: ", req)
logger.info("host name :",req.headers.host)
const input = 'input' in req.query ? req.query.input : ''
if (input.length == 0) {
logger.error('User requested echo for empty string!')
res.send('You must supply an input string!')
} else {
logger.info(`User requested echo for string ${input}`)
if (config.persist) {
const collection = db.collection('echos');
collection.insertOne({
time: Date.now(),
input
})
}
res.send(`Echo says: ${input}`)
}
}
app.get('/', (req, res) => echo(req, res))
app.listen(config.port, () => logger.info(`Echo listening on port ${config.port}!`))