-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
53 lines (47 loc) · 1.27 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
const remoteHost = "https://registry.npmjs.org/"
const headers = {}
var restify = require('restify');
var fetch = require('isomorphic-fetch')
var url = require('url')
var fs = require('fs')
var server = restify.createServer({
name: 'deptree',
version: '1.0.0'
});
server.use(restify.acceptParser(server.acceptable));
server.use(restify.queryParser());
server.use(restify.bodyParser());
server.use(
function crossOrigin(req,res,next){
res.header("Access-Control-Allow-Origin", "*")
res.header("Access-Control-Allow-Headers", "X-Requested-With")
return next()
}
)
// push /api to the registry
server.get('/api/.*', function(req, res, next) {
// shift /api and reconstitute
var endpoint = url.parse(req.url).path.split("/").splice(2).join("/")
console.log(endpoint)
var response = fetch(`${remoteHost}${endpoint}`,{
headers: headers
})
.then((response) => {
response.json().then((json) => {
res.send(json)
return next()
})
})
});
// serve root page
server.get('/', restify.serveStatic({
directory: './public',
file: 'index.html'
}));
// serve static files
server.get(/\/public\/?.*/, restify.serveStatic({
directory: __dirname
}));
server.listen(8080, function () {
console.log('%s listening at %s', server.name, server.url);
});