All Projects → davideast → hnpwa-api

davideast / hnpwa-api

Licence: other
CDN cached Hacker News API

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to hnpwa-api

Hacker News Resolve
React & Redux & Resolve implementation of Hacker News
Stars: ✭ 79 (+8.22%)
Mutual labels:  hacker-news
Materialistic
A material-design Hacker News Android reader
Stars: ✭ 2,163 (+2863.01%)
Mutual labels:  hacker-news
Devo
A Chrome and Firefox extension that displays GitHub Trending, Hacker News, Product Hunt and Designer News on every new tab.
Stars: ✭ 236 (+223.29%)
Mutual labels:  hacker-news
Quiet Hacker News
😶 Quiet Hacker News
Stars: ✭ 100 (+36.99%)
Mutual labels:  hacker-news
Flews
A multi-service news app written in Flutter
Stars: ✭ 148 (+102.74%)
Mutual labels:  hacker-news
Hackernews.el
Hacker News client for Emacs
Stars: ✭ 200 (+173.97%)
Mutual labels:  hacker-news
Hack The Media
This repo collects examples of intentional and unintentional hacks of media sources
Stars: ✭ 1,194 (+1535.62%)
Mutual labels:  hacker-news
container-builder-github-ci-status
Google Cloud Function responds to PubSub events on the cloud-builds topic to update GitHub CI status.
Stars: ✭ 23 (-68.49%)
Mutual labels:  cloud-functions
Hackerweb Native
A simply readable Hacker News app for iOS & Android, built with React Native. V2 now over here https://github.com/cheeaun/hackerweb-native-2
Stars: ✭ 177 (+142.47%)
Mutual labels:  hacker-news
Hackerbuzz Reactnative
A Hacker News Reader built in React Native for iOS and Android
Stars: ✭ 220 (+201.37%)
Mutual labels:  hacker-news
Hackernewsbot
📰 Telegram bot that posts new hot stories from Hacker News to telegram channel
Stars: ✭ 103 (+41.1%)
Mutual labels:  hacker-news
Hackernew
The best Chrome extension to make Hacker News quicker and more useful.
Stars: ✭ 120 (+64.38%)
Mutual labels:  hacker-news
What Hn Says Webext
Web Extension: Easily find Hacker News discussions about the page you're currently browsing.
Stars: ✭ 214 (+193.15%)
Mutual labels:  hacker-news
Hackernews
A Hacker News reader iOS app written in Swift.
Stars: ✭ 1,251 (+1613.7%)
Mutual labels:  hacker-news
Hnpwa Vanilla
Hacker News PWA implemented using no framework just javascript
Stars: ✭ 245 (+235.62%)
Mutual labels:  hacker-news
Hackernewsclient Ios
An iOS client for Hacker News
Stars: ✭ 77 (+5.48%)
Mutual labels:  hacker-news
React Hn
React-powered Hacker News client
Stars: ✭ 2,174 (+2878.08%)
Mutual labels:  hacker-news
cloud-functions-boilerplate
An ever-evolving, opinionated architecture, starter kit, and development environment for writing and structuring google cloud functions for firebase.
Stars: ✭ 40 (-45.21%)
Mutual labels:  cloud-functions
Awesome Hacker News
Awesome Hacker News: a collection of awesome Hacker News apps, libraries, resources and shiny things.
Stars: ✭ 250 (+242.47%)
Mutual labels:  hacker-news
Clojurenews
Clojure News Web Application - (Hacker News Clone)
Stars: ✭ 217 (+197.26%)
Mutual labels:  hacker-news

HNPWA API

Deploy a CDN cached Hacker News API to your own Firebase Hosting Domain. All in two lines of code 😎

Heavily inspired/guided by cheeaun's node-hnapi.

Install

npm i hnpwa-api

Basic usage

Import and use in your functions/index.js file:

const hnapi = require('hnpwa-api');
exports.api = hnapi.trigger({
   useCors: false, // defaults to false
   useCompression: true, // defaults to true
   browserCacheExpiry: 300, // in seconds (5 min is the default)
   cdnCacheExpiry: 600, // in seconds (10 min is the default)
   staleWhileRevalidate: 120, // Allow CDN to serve stale data 120 seconds after cdnCacheExpiry
   firebaseAppName: 'hnpwa-api', // defaults to hnpwa-api
   offline: false, // Serves offline data if data is downloaded (See Global Module guide)
   routerPath: 'api', // provide a serving path ex: mysite.com/api/news.json
});

Why?

Two reasons: latency and same domain.

Latency

This API is designed for Firebase Hosting which is backed by a global CDN. Responses are cached in edges around the globe which results in low latency.

Latency test: CDN cached vs. in memory cache

Same domain

With HTTP/2 you reuse one connection per domain. This package allows you to easily deploy your own HNAPI on your own domain for one nice TCP connection.

Setup Tutorial

1. Install the Firebase CLI:

npm i -g firebase-tools

2. Initialize Cloud Functions for Firebase

firebase init functions

3. Install hnpwa-api

Inside of the functions folder, install hnpwa-api:

cd functions
npm i hnpwa-api --save # not needed on npm 5 but you get what im sayin

4. Add HNAPI endpoint

Open functions/index.js, and configure your HNAPI.

const hnapi = require('hnpwa-api');
exports.api = hnapi.trigger({
   useCors: false, // defaults to false
   useCompression: true, // defaults to true
   browserCacheExpiry: 300, // in seconds (5 min is the default)
   cdnCacheExpiry: 600, // in seconds (10 min is the default)
   staleWhileRevalidate: 120, // Allow CDN to serve stale data 120 seconds after cdnCacheExpiry
   firebaseAppName: 'hnpwa-api', // defaults to hnpwa-api
   offline: false, // Serves offline data if data is downloaded (See Global Module guide)
   routerPath: 'api', // provide a serving path ex: mysite.com/api/news.json
});

5. Initialize Firebase Hosting

firebase init hosting

6. Create a redirect for the API function

Open firebase.json and create a redirect to call out to the HNAPI:

{
  "hosting": {
    "public": "public",
    "rewrites": [{
       "source": "**",
       "function": "api"
    }]
  }
}

7. Deploy!

firebase deploy

That's all there is to it. Feel free to file an issue if you find a bug.

Global Module use

The hnpwa-api module can either be downloaded as a global module or used from the node_modules/.bin/hnpwa-api directory.

Serving offline

The global module provides the ability to save data locally for offline serving. If you're developing on a bus, airplane, or someother place without a connection you'll need this.

# 1) Save to node_modules/hnpwa-api/offline (~10mb)
node_modules/.bin/hnpwa-api --save
# 2) Now serve offline
node_modules/.bin/hnpwa-api --serve --offline

Non-Firebase setup

Not using Cloud Functions or Firebase Hosting as your backend? No problem. This library still has you covered.

const hnapi = require('hnpwa-api');
// does not include any middleware like the trigger() call above
const expressApp = hnapi.app(); // optionally provide a firebase app name
expressApp.listen(3000, () => console.log('Listening all on my own!'));

This returns an express app instance with the expected HN API endpoints. No middleware is attached unlike the trigger(config) method.

Why do I need a Firebase App Name if I'm not using Firebase Hosting?

You may not use Firebase as your backend, but Hacker News does. The base HN API is backed by the Firebase Database. This library uses the Firebase Node SDK to retrieve data and coalesce it into a single UI friendly response.

Contribute!?

git clone https://github.com/davideast/hnpwa-api/
npm i
npm run build # single build of the project
npm run watch # typescript (tsc) watcher
npm run serve # local node debug server
npm run pack # local tarball for test installations
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].