<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Emily</title>
    <description>The latest articles on DEV Community by Emily (@emilybonar).</description>
    <link>https://dev.to/emilybonar</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F438856%2F94d2c0e2-cf90-4066-85cf-3615196fa7aa.png</url>
      <title>DEV Community: Emily</title>
      <link>https://dev.to/emilybonar</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/emilybonar"/>
    <language>en</language>
    <item>
      <title>Writing my first database application with Next.js and Heroku</title>
      <dc:creator>Emily</dc:creator>
      <pubDate>Fri, 08 Jan 2021 02:08:12 +0000</pubDate>
      <link>https://dev.to/emilybonar/writing-my-first-database-application-with-next-js-and-heroku-2d8a</link>
      <guid>https://dev.to/emilybonar/writing-my-first-database-application-with-next-js-and-heroku-2d8a</guid>
      <description>&lt;p&gt;Up until now I've been solely making static sites, so I decided it was time to branch out and try to make a full stack application with a database. I decided to make a simple Twitter clone - sending and receiving short messages to and from the database, how hard could it be? I'll be attempting to write a step by step guide to what I did in the hopes that it could help someone else.&lt;/p&gt;

&lt;p&gt;First off I went to &lt;a href="https://www.heroku.com"&gt;Heroku&lt;/a&gt; and created an account. I'm not looking to spend any money on this, so I chose all the free tier options. After I made a new blank app, I connected it to a GitHub repository and set it to automatic deployment from the main branch. &lt;/p&gt;

&lt;p&gt;In that repository I set up a basic &lt;a href="https://nextjs.org/"&gt;Next.js&lt;/a&gt; app using &lt;code&gt;npx create-next-app&lt;/code&gt; At this point I ran into a problem. In order to get my app to work, I had to change the start script in package.json from &lt;code&gt;"start": "next start"&lt;/code&gt; to &lt;code&gt;"start": "next start -p $PORT"&lt;/code&gt;. At that point, you can write React as normal and have it hosted on Heroku.&lt;/p&gt;

&lt;p&gt;Now that I had a basic front-end application running, I wanted to connect the back-end. This is where I had some trouble, as I had never done it before. I went to the resources tab on Heroku and searched Postgres, then added Heroku Postgres to my application at the Hobby Dev - Free tier. Then I went and downloaded the latest release of &lt;a href="https://www.postgresql.org/"&gt;PostgreSQL&lt;/a&gt; to my machine so I could develop with it.&lt;/p&gt;

&lt;p&gt;After it installed (using all default settings), I launched pgAdmin 4 to monitor and edit the Postgres server running locally. (Side note, in order to get pgAdmin running on my Windows machine I had to edit a registry entry, &lt;a href="https://stackoverflow.com/questions/64829748/pgadmin-is-not-loading"&gt;here's the details&lt;/a&gt;). You need to make sure you know your login role and password. You should create a new database by right clicking on Databases under the PostgreSQL server and set that up with a name and put your login role as owner.&lt;/p&gt;

&lt;p&gt;At this point, I started to use &lt;a href="https://www.prisma.io/"&gt;Prisma&lt;/a&gt; to connect my app to the database. Install it by running &lt;code&gt;npm install @prisma/cli -D&lt;/code&gt; and then &lt;code&gt;npx prisma init&lt;/code&gt; in your base repository directory. A .env file should be created where you want to set &lt;code&gt;DATABASE_URL="postgresql://[username]:[password]@localhost:[server port]/[database name]"&lt;/code&gt;. Then you fill in your prisma/schema.prisma file. This is what I used, but you can change the name and contents of the models depending on what you want to store in your database. &lt;a href="https://www.prisma.io/docs/concepts/components/prisma-schema"&gt;Here's&lt;/a&gt; a reference on the schema.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

generator client {
  provider = "prisma-client-js"
}

model User {
  name  String @id
  posts Post[]
}

model Post {
  id        Int @id @default(autoincrement())
  createdAt DateTime @default(now())
  content   String 
  author    User
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then you can run &lt;code&gt;prisma migrate dev --preview-feature&lt;/code&gt; in order to push your data model to the database! In order to visualize and add sample data to the database, I used &lt;a href="https://github.com/prisma/studio/releases"&gt;Prisma Studio&lt;/a&gt;, which you just connect to your schema.prisma file and then can add and delete records to your heart's content. In order to get this to work on your Heroku server you need to push your database schema to Heroku with the commands &lt;code&gt;heroku login&lt;/code&gt; and then &lt;code&gt;heroku pg:push [local database name] [heroku database name] --app [heroku app name]&lt;/code&gt;. You can find more details on this command &lt;a href="https://devcenter.heroku.com/articles/heroku-postgresql#pg-push-and-pg-pull"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Now that the database is set up, let's figure out how to read and write to it. Next.js has &lt;a href="https://nextjs.org/docs/api-routes/introduction"&gt;API routes&lt;/a&gt; that can handle this! First, I had to install middleware to handle CORS for me with &lt;code&gt;npm i cors&lt;/code&gt;. I was having trouble with CORS only on mobile devices, and this fixed it. I also had to add the following file at api/_base.js. This meant that I wasn't creating new database sessions with every query.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { PrismaClient } from "@prisma/client";

export let prisma;

if (process.env.NODE_ENV === "production") {
    prisma = new PrismaClient();
} else {
    if (!global.prisma) {
        global.prisma = new PrismaClient();
    }

    prisma = global.prisma;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create the path pages/api/posts/index.js, and enter the following code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { prisma } from "../_base";
import Cors from "cors";

// Initializing the cors middleware
const cors = Cors({
    methods: ["GET", "HEAD"],
});

// Helper method to wait for a middleware to execute before continuing
// And to throw an error when an error happens in a middleware
function runMiddleware(req, res, fn) {
    return new Promise((resolve, reject) =&amp;gt; {
        fn(req, res, (result) =&amp;gt; {
            if (result instanceof Error) {
                return reject(result);
            }

            return resolve(result);
        });
    });
}

export default async function handle(req, res) {
    await runMiddleware(req, res, cors);
    const posts = await prisma.post.findMany();
    res.setHeader("Cache-Control", "public, max-age=0, stale-while-revalidate=1");
    res.json(posts);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now when you visit example.herokuapp.com/api/posts, it will return a JSON document containing every Post item in your database!&lt;/p&gt;

&lt;p&gt;Writing to the database is simple too (though not simple to figure out). My MessageInput component has the following function to send data to the API route and then clear the form.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function postMessage(e) {
    e.preventDefault();
    let content = document.querySelector("#text");
    let author = document.querySelector("#name");
    fetch(`${server}/api/posts/write`, {
        method: "POST",
        headers: {
            "Content-Type": "application/json",
        },
        body: JSON.stringify({ content: content.value, author: author.value }),
    })
        .catch((error) =&amp;gt; console.error("WriteError", error))
        .finally(() =&amp;gt; {
            content.value = "";
            author.value = "";
        });
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And then this code at api/posts/write.js to handle it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { prisma } from "../_base";
import Cors from "cors";

// Initializing the cors middleware
const cors = Cors({
    methods: ["GET", "HEAD"],
});

// Helper method to wait for a middleware to execute before continuing
// And to throw an error when an error happens in a middleware
function runMiddleware(req, res, fn) {
    return new Promise((resolve, reject) =&amp;gt; {
        fn(req, res, (result) =&amp;gt; {
            if (result instanceof Error) {
                return reject(result);
            }

            return resolve(result);
        });
    });
}

export default async function handle(req, res) {
    await runMiddleware(req, res, cors);
    const user = await prisma.post.create({
        data: {
            content: req.body.content,
            author: {
                connectOrCreate: {
                    where: { name: req.body.author },
                    create: { name: req.body.author },
                },
            },
        },
    });
    res.json({ user: user });
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After the middleware, you're creating a post, then connecting it to an existing author in the database, or creating one if it doesn't exist already.&lt;/p&gt;

&lt;p&gt;And then you have a working website! There are all sorts of improvements and features you can add, but having reading and writing to the database taken care of means you're well on your way!&lt;/p&gt;

&lt;p&gt;View my example running live (and leave a comment!) here: &lt;a href="https://flibberty-gibbets.herokuapp.com/"&gt;https://flibberty-gibbets.herokuapp.com/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>nextjs</category>
      <category>postgres</category>
      <category>javascript</category>
      <category>heroku</category>
    </item>
    <item>
      <title>Netlify Redirects and package.json</title>
      <dc:creator>Emily</dc:creator>
      <pubDate>Sun, 03 Jan 2021 22:58:40 +0000</pubDate>
      <link>https://dev.to/emilybonar/netlify-redirects-and-package-json-3h0e</link>
      <guid>https://dev.to/emilybonar/netlify-redirects-and-package-json-3h0e</guid>
      <description>&lt;p&gt;I use &lt;a href="https://www.netlify.com/"&gt;Netlify&lt;/a&gt; to host my web projects, and I've been running into a consistent but confusing problem. Every time it happens, I forget the solution and have to rediscover it, so I'm hoping that writing this reminds me, and potentially helps anyone else facing the same issue.&lt;/p&gt;

&lt;p&gt;I create a React app, and host it at &lt;code&gt;projectName.example.com&lt;/code&gt;. No problems there! However, then I create a redirect that sends it to &lt;code&gt;example.com/projectName/&lt;/code&gt;. This is because I don't want to buy new domains for each project, and find subdomains a little ugly. However, now when I try to visit &lt;code&gt;example.com/projectName/&lt;/code&gt;, I get a blank screen! More specifically, I get a webpage that is only comprised of the contents of the public/ folder.&lt;/p&gt;

&lt;p&gt;The solution to this is adding a line to your package.json file. Specifically, the line &lt;code&gt;"homepage": ".",&lt;/code&gt;. This allows the web browser to locate the bundled files that it wasn't able to find before. Why this happens I don't know! If anyone knows why, please leave it in the comments below.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>netlify</category>
      <category>react</category>
    </item>
  </channel>
</rss>
