Skip to content

Skidragon/population-viewer

Repository files navigation

Site Image(s)

Image of landing page

What I Learned

- The basics of Next.js.
- How to setup routes and middleware.
- How to deploy to Vercel.
- How to rate limit, slow down requests, and do internal caching.
- How to web scrape with cheerio.
- How to work with svgs.
- Using d3.js to create a horizontal bar chart.
- The terms domain and range.
- Updating a favicon.

Code Highlights

Bar Chart

<svg
  width={"100%"}
  height={height}
  style={{
    ...style,
    background: "#F5F3F2",
  }}
>
  <g transform={`translate(${margin.left},${margin.top})`}>
    <g transform={`translate(0,-25)`}>
      <YAxis yScale={yScale} tickValueFormat={(tickValue) => `${tickValue}`} />
    </g>
    {isWide ? (
      <XAxis
        xScale={xScale}
        innerHeight={innerHeight}
        tickValueFormat={(tickValue) => xAxisTickFormat(tickValue)}
      />
    ) : null}
    <Bars
      data={data}
      xScale={xScale}
      yScale={yScale}
      yValue={(d) => d.country}
      xValue={(d) => d.population}
      tooltipFormat={(d) => xAxisTickFormat(d.population)}
    />
  </g>
</svg>

Scraping Population data

const pageURLToScrape =
  "https://www.worldometers.info/world-population/population-by-country/";
const getPopulationByCountry = async () => {
  const { data: pageHTML } = await axios.get(pageURLToScrape);
  const $ = cheerio.load(pageHTML);
  const table = $("#example2");
  const collection = [];
  table.find("tbody tr").each((i, row) => {
    const $row = $(row);
    const country = {};
    const labels = ["rank", "country", "population"];
    //Get Values out of cells
    const tds = $row.find("td");
    const $tds = $(tds);
    const values = [];
    $tds.each((j, td) => {
      const $td = $(td);
      values.push($td.text());
    });
    for (let i = 0; i < labels.length; i++) {
      let label = labels[i];
      let value = values[i];
      country[label] = value;
    }
    collection.push(country);
  });
  return collection;
};

Caching, Rate Limiting, and Slow Down

export default async (req, res) => {
  if (req.method === "GET") {
    try {
      await runMiddleware(req, res, limiter);
      await runMiddleware(req, res, speedLimiter);
      const cacheKey = "population-by-country";
      if (myCache.has(cacheKey)) {
        console.log(myCache.has(cacheKey));
        return res.status(200).json(myCache.get(cacheKey));
      } else {
        const populations = await getPopulationByCountry();
        myCache.set(cacheKey, populations);
        return res.status(200).json(populations);
      }
    } catch (err) {}
  }
};

This is a Next.js project bootstrapped with create-next-app.

Getting Started

First, run the development server:

npm run dev
# or
yarn dev

Open http://localhost:3000 with your browser to see the result.

You can start editing the page by modifying pages/index.js. The page auto-updates as you edit the file.

API routes can be accessed on http://localhost:3000/api/hello. This endpoint can be edited in pages/api/hello.js.

The pages/api directory is mapped to /api/*. Files in this directory are treated as API routes instead of React pages.

Learn More

To learn more about Next.js, take a look at the following resources:

You can check out the Next.js GitHub repository - your feedback and contributions are welcome!

Deploy on Vercel

The easiest way to deploy your Next.js app is to use the Vercel Platform from the creators of Next.js.

Check out our Next.js deployment documentation for more details.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published