forked from RayMaAU/github-actions-workflow-simple-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
31 lines (23 loc) · 920 Bytes
/
server.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
var http = require('http');
var mongoClient = require('mongodb').MongoClient;
var mongoDBUri = process.env.MONGODB_URI || '';
var port = process.env.PORT || 80;
mongoClient.connect(mongoDBUri, function (err, client) {
http.createServer(function (req, res) {
if(err){
res.writeHead(500, { 'Content-Type': 'text/plain' });
res.end(`Failed to connect to the database.\n${err}`)
return;
}
const db = client.db('logs');
const requests = db.collection('requests');
requests.count(function(error, count){
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end(`Hello World!\nThere are ${count} request records.`);
})
requests.insertOne({
ip: req.headers['x-forwarded-for'] || req.connection.remoteAddress,
time: Date.now()
});
}).listen(port);
});