This repository was archived by the owner on Feb 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
60 lines (52 loc) · 1.72 KB
/
index.js
File metadata and controls
60 lines (52 loc) · 1.72 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
const express = require('express');
const request = require('request');
const app = express();
app.set('port', (process.env.PORT || 3000));
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", '*');
res.header("Access-Control-Allow-Credentials", true);
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header("Access-Control-Allow-Headers", 'Origin,X-Requested-With,Content-Type,Accept,content-type,application/json');
next();
});
app.get('/', function(req, res) {
const url = req.protocol + '://' +req.hostname;
const message = `<h1>GitHub API for commit count</h1>
<p>
<a href="https://github.com/9sako6/github-api-for-commit-count">9sako6/github-api-for-commit-count</a>
</p>
<h2>How to use</h2>
<p>
${url}/count?user=[username]&repo=[repository]
</p>`;
res.send(message);
});
app.get('/count', function(req, res) {
const user = req.query.user || '';
const repo = req.query.repo || '';
request(`https://github.com/${user}/${repo}`, (e, response, body) => {
if (response.statusCode === 404) {
console.error('Not Found');
res.status(404).end();
return;
} else if (response.statusCode !== 200) {
console.error(e);
res.status(response.statusCode).end();
return;
}
try {
const commitNum = body
.replace(/\r?\n/g, '')
.match(/<span class="d-none d-sm-inline">[\s\t]*<strong>([\d,]+)/)[1]
.replace(/,/, '');
res.send(commitNum);
} catch(error) {
console.error(error);
res.status(500).end();
return;
}
});
});
app.listen(app.get('port'), function() {
console.log("Node app is running at localhost:" + app.get('port'));
});