Skip to content

Commit 18e67f0

Browse files
committed
feat: Add examples
1 parent 7423382 commit 18e67f0

File tree

7 files changed

+141
-1
lines changed

7 files changed

+141
-1
lines changed

β€Žexamples/express/server.jsβ€Ž

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
'use strict'
2+
3+
const path = require('path')
4+
const express = require('express')
5+
const firebase = require('firebase')
6+
const hackernews = require('../../')
7+
8+
const server = express()
9+
const hnservice = hackernews.init(firebase, {
10+
log: console.log
11+
})
12+
const watchmode = process.argv.includes('--watch')
13+
const index = `
14+
<ul>
15+
<li><a href="/hackernews/top">top</a></li>
16+
<li><a href="/hackernews/top/1>top at page 1</a></li>
17+
<li><a href="/hackernews/new">new</a></li>
18+
<li><a href="/hackernews/best">best</a></li>
19+
<li><a href="/hackernews/ask">ask</a></li>
20+
<li><a href="/hackernews/show">show</a></li>
21+
<li><a href="/hackernews/job">job</a></li>
22+
<li><a href="/hackernews/kids/14545382">kids/14545382</a></li>
23+
<li><a href="/hackernews/length/top">length/top</a></li>
24+
<li><a href="/hackernews/user/jl">users/jl</a></li>
25+
</ui>`
26+
27+
server.get('/', (req, res) => {
28+
res.send(index)
29+
})
30+
31+
server.get('/hackernews/*', (req, res) => {
32+
hnservice.fetch(req.path)
33+
.then(data => {
34+
res.send(typeof data === "number" ? String(data) : data)
35+
})
36+
.catch(err => {
37+
console.error(err)
38+
res.status(500).send(err.toString())
39+
})
40+
})
41+
42+
Promise.resolve(watchmode && hnservice.watch()).then(() => {
43+
const PORT = 3001
44+
server.listen(PORT, () => {
45+
console.log(`server has started with ${watchmode ? 'watch' : 'fetch'} mode.` +
46+
` visit to http://localhost:${PORT}`)
47+
})
48+
})

β€Žexamples/react/.gitignoreβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# See https://help.github.com/ignore-files/ for more about ignoring files.
22

33
# dependencies
4-
/node_modules
4+
node_modules
55

66
# testing
77
/coverage

β€Žexamples/react/src/App.jsβ€Ž

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import React, { Component } from 'react';
2+
import firebase from 'firebase'
3+
import hackernews from 'firebase-hackernews'
4+
import logo from './logo.svg';
5+
import './App.css';
6+
7+
class App extends Component {
8+
constructor() {
9+
super();
10+
11+
this.state = {
12+
top: []
13+
};
14+
15+
this.hnservice = hackernews.init(firebase)
16+
}
17+
18+
async componentDidMount() {
19+
const top = await this.hnservice.stories('top', {page: 1, count: 40})
20+
this.setState({top: top})
21+
}
22+
23+
render() {
24+
const liStyle = {
25+
listStyleType: 'none',
26+
textAlign: 'left',
27+
paddingBottom: '5px'
28+
};
29+
30+
const top = this.state.top.map(t => (
31+
<li key={t.id} style={liStyle}>
32+
<a href={t.url}>{t.title}</a> - {t.by}
33+
</li>
34+
));
35+
36+
return (
37+
<div className="App">
38+
<div className="App-header">
39+
<img src={logo} className="App-logo" alt="logo" />
40+
<h2>Hacker News - Top</h2>
41+
</div>
42+
<p className="App-intro">
43+
<ul>
44+
{top}
45+
</ul>
46+
</p>
47+
</div>
48+
);
49+
}
50+
}
51+
52+
export default App;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import React from 'react';
2+
import ReactDOM from 'react-dom';
3+
import App from './App';
4+
5+
it('renders without crashing', () => {
6+
const div = document.createElement('div');
7+
ReactDOM.render(<App />, div);
8+
});

β€Žexamples/react/src/index.jsβ€Ž

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import React from 'react';
2+
import ReactDOM from 'react-dom';
3+
import App from './App';
4+
import './index.css';
5+
6+
ReactDOM.render(<App />, document.getElementById('root'));
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const path = require('path')
2+
const express = require('express')
3+
4+
const server = express()
5+
const serve = (subpath, cache) => express.static(
6+
path.resolve(__dirname, subpath),
7+
{maxAge: cache && !dev ? 1000 * 60 * 60 * 24 * 30 : 0}
8+
)
9+
10+
server.use('/firebase-hackernews-sw.js', serve('../../dist/firebase-hackernews-sw.js'))
11+
12+
server.use(serve('./'))
13+
14+
server.listen(3000, (err) => {
15+
if (err) throw err
16+
console.log('> Ready on http://localhost:3000')
17+
})
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/* global importScripts hackernews */
2+
importScripts('https://www.gstatic.com/firebasejs/4.1.2/firebase-app.js')
3+
importScripts('https://www.gstatic.com/firebasejs/4.1.2/firebase-database.js')
4+
importScripts('/firebase-hackernews-sw.js')
5+
6+
hackernews.init(firebase, {
7+
log: console.log,
8+
watch: true
9+
})

0 commit comments

Comments
Β (0)