-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.js
executable file
·56 lines (48 loc) · 1.15 KB
/
app.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
'use strict'
const ejs = require('ejs')
const express = require('express')
const ipfsAPI = require('ipfs-api')
const libraries = require('./libraries.json');
const config = {
production: {
ipfsHost: 'ipfs.io',
ipfsPort: 80,
baseUrl: 'http://aono.2015.nodeknockout.com'
},
dev: {
ipfsHost: 'localhost',
ipfsPort: 5001,
baseUrl: 'http://localhost',
port: 8081
}
}
const cfg = config[process.env.NODE_ENV] || config.dev
const ipfs = ipfsAPI(cfg.ipfsHost, cfg.ipfsPort)
const app = express()
app.get('/', (req, res) => {
res.render('index', {
libraries,
baseUrl: cfg.baseUrl,
port: cfg.port
})
})
app.get('/:hash', (req, res) => {
ipfs.cat(req.params.hash, (err, file) => {
if (err || !file) {
return console.error(err)
}
if(file.readable) {
file.pipe(res)
} else {
res.send(file)
}
res.setHeader("Access-Control-Allow-Origin", "*")
})
})
const port = process.env.PORT || 8081
app.set('views', './views')
app.set('view engine', 'html');
app.engine('html', ejs.renderFile);
app.set('x-powered-by', false)
app.listen(port)
console.log('Express server started on port %s', port)