showing results for - "create gatsby pages node js"
Emilie
18 Oct 2016
1const axios = require('axios');
2
3require("dotenv").config({
4  path: `.env.${process.env.NODE_ENV}`
5});
6
7const getRepositoryData = async () => {
8  console.log(process.env.API_URL);
9  return axios.get(`${process.env.API_URL}/repositories`);
10};
11
12exports.createPages = async ({
13  actions: {
14    createPage
15  }
16}) => {
17  let repositories = await getRepositoryData();
18  repositories = repositories.data;
19
20  // Create a page that lists all repositories.
21  createPage({
22    path: `/`,
23    component: require.resolve('./src/templates/all-repositories.js'),
24    context: {
25      repositories
26    }
27  });
28
29  // Create a page for each repository.
30  repositories.forEach(repository => {
31    createPage({
32      path: `/repository/${repository.owner}/${repository.name}`,
33      component: require.resolve('./src/templates/repository.js'),
34      context: {
35        repository
36      }
37    });
38  });
39};
40