Skip to content

Commit

Permalink
Change API endpoint to /api
Browse files Browse the repository at this point in the history
  • Loading branch information
sashachabin committed Jul 27, 2022
1 parent 01a092e commit 663d44a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
18 changes: 11 additions & 7 deletions client/view/browsers/index.js
@@ -1,7 +1,7 @@
import wikipediaLinks from './wikipedia-links.json'

// TODO Add variables to .env
const API_HOST = 'http://localhost:5000'
// TODO Putting client and server to the single Docker image to use the same domain and re-use the power of HTTP/2
const API_HOST = 'http://localhost:5000/api'
const WIKIPEDIA_URL = 'https://en.wikipedia.org/wiki/'

document.getElementById('browsers-input').addEventListener('input', () => {
Expand All @@ -15,9 +15,11 @@ sendQuery('defaults')

async function sendQuery(query) {
// TODO Handling errors: 400 status and connection errors

let response = await fetch(`${API_HOST}/?q=${encodeURIComponent(query)}`)
let { browsers, versions: { browserslist, caniuse } } = await response.json()
let response = await fetch(`${API_HOST}?q=${encodeURIComponent(query)}`)
let {
browsers,
versions: { browserslist, caniuse }
} = await response.json()

document.getElementById('browsers-root').innerHTML = `
<ul class="browsers">
Expand All @@ -26,7 +28,9 @@ async function sendQuery(query) {
({ id, name, versions }) => `
<li class="browsers__item">
<img src="/${id}.png" alt="" />
<a href="${getWikipediaLink(id)}" target="_blank" rel="noreferrer noopener">${name}</a>
<a href="${getWikipediaLink(
id
)}" target="_blank" rel="noreferrer noopener">${name}</a>
<ul>
${Object.entries(versions)
Expand All @@ -52,5 +56,5 @@ async function sendQuery(query) {
}

function getWikipediaLink(id) {
return WIKIPEDIA_URL + wikipediaLinks[id];
return WIKIPEDIA_URL + wikipediaLinks[id]
}
3 changes: 2 additions & 1 deletion server/index.js
Expand Up @@ -14,7 +14,8 @@ http
.createServer(async (req, res) => {
let url = new URL(req.url, `http://${req.headers.host}/`)

if (url.pathname === '/') {
// TODO Putting client and server to the single Docker image to use the same domain and re-use the power of HTTP/2
if (url.pathname === '/api') {
let query = url.searchParams.get('q') || DEFAULT_QUERY
let queryWithoutQuotes = query.replace(/'/g, '')

Expand Down

0 comments on commit 663d44a

Please sign in to comment.