Site: Population Viewer
- 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.
<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>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;
};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.
First, run the development server:
npm run dev
# or
yarn devOpen 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.
To learn more about Next.js, take a look at the following resources:
- Next.js Documentation - learn about Next.js features and API.
- Learn Next.js - an interactive Next.js tutorial.
You can check out the Next.js GitHub repository - your feedback and contributions are welcome!
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.