- Cheat Sheet: Setting up Express with Postgres via Knex Read other posts ← More Learning Resources During COVID-19 Outbreak Part 3 TIL About Streamlit's Magic →.
- Knex cheatsheet PostgreSQL JSON cheatsheet PostgreSQL cheatsheet Top cheatsheets. Elixir cheatsheet ES2015+ cheatsheet React.js cheatsheet Vimdiff cheatsheet Vim cheatsheet.
About the author
Telmo Goncalves is a software engineer with over 13 years of software developmentexperience and an expert in React. He’s currently Engineering Team Lead at Marley Spoon.
Check out more of his work on telmo.im
K'NEX 3D tricks hints guides reviews promo codes easter eggs and more for android application. Avoid K'NEX 3D hack cheats for your own safety, choose our tips and advices confirmed by pro players, testers and users like you. Ask a question or add answers, watch video tutorials &.
When I was building my own website and blog, I chose to use Markdown in conjunction with Next.jsbecause it’s easy, fast, and, to be honest, I enjoy writing in Markdown. In this article, I’ll walk through how I didthis so you can produce a great, content-driven site of your own.
This article assumes the reader is generally unfamiliar with setting up web applications with tools like npm and thesystem Terminal. Several steps involve encountering errors and backtracking; this was intentional for the purposes ofeducating the reader on how the application is architected and the interdependencies of each component.
Those more familiar will be able to skip through some of the explanatory details for each step.
First, create a folder for your project. Do this by running:
For those new to UNIX commands, this produces a new directory named ‘blog’ (mkdir blog
) and navigates to it (cd blog
).
Next, install the following dependencies using:
Running npm init
will create a package.json
file in our project, using -y
will skip npm’s default questions. Ifyou like to include this information, just remove it.
Since the site will use next
, the scripts in package.json
will need to be changed to the following:
Next, run npm run dev
which should start the project on port 3000
. Open your browser and navigate tohttp://localhost:3000
.
At this point, an error may be thrown (or the build didn’t complete at all). This is because Next.js is expecting tofind a pages
directory. This is where you’ll create content pages.
Create the pages
directory with an index.js
file by running the following:
Brackets for mac os x. Now if you run npm run dev
and open http://localhost:3000
in your browser, the following error should be shown:
This is because the index.js
file exists, but it’s empty.
First, create a React component index.js
at the top of the file:
Once saved, Welcome to the Homepage! should be displayed on http://localhost:3000
.
You can create many pages as you want (ie - author.js
).
Do this by running:
And adding a React component (shown using author.js
):
Save, and you’ll be able to see the content by opening http://localhost:3000/author
in your browser.
Here, we’ll dive into how getInitialProps
from Next.js works. Above the export default Homepage
line on the index .js
homepage, add:
Collective, it should look like this:
Props (which stands for “property”) are used for passing data from one component to another.
Here a prop is created called blogTitle
and passed into the Homepage
component. When you accesshttp://localhost:3000
, the content should now read: Welcome to the blog: Rookie for life!
The app is passing a prop saying what the value of blogTitle
is. If you change it from Rookie for life! toRockstar! and save, you should see the page update with the new title.
Next, let’s create blog articles using Markdown .md
files and dynamically load them. Using Markdown files for thecontent is beneficial because it’s easier to write in them and manage the content.
That way, when a user opens a page like http://localhost:3000/blog/article-name
it will render with content writtenin an article-name.md
file.
A great feature of Next.js is that it allows developers to create dynamic files.
Create a dynamic file called [slug].js
in a pages/posts/
directory:
Note that the posts
directory will be inside of the pages
directory.
Now create a posts
template using a React component:
This queries the URL and extracts the slug
from it. slug
is used because that’s the name of the file ([slug].js
). If the file were named [id].js
, the query would be context.query.id
instead.
Now, if you access http://localhost:3000/posts/hello-world
you should see: Here we’ll load “hello-world”
If you change hello-world
to in the URL awesome-nextjs-blog
, the page will display: Here we’ll load“awesome-nextjs-blog”
The content Markdown files should be stored in a separate directory so they’re easier to manage. Create a content
directory and a Markdown file by running:
Upon opening the content
directory, there should be a blank awesome-nextjs-blog.md
file.
Open the awesome-nextjs-blog.md
file and add the following:
Now open [slug].js
and add the following above return { slug }
:
So collectively, it looks like this:
The application is now querying slug
from the URL and loading an .md
file with that slug.
Markdown files contain frontmatter
data. This is information defined in the section divided off using the ---
markers in the .md
file. To parse that in some way. To parse that, use gray-matter.
This can be installed by running:
Now update the [slug].js
template to import matter
and parse the .md
content:
If you reload the page, you should see an error: TypeError: expected input to be a string or buffer
This will need to be resolved by adding a Next.js configuration file. Do this by running:
Open the newly created next.config.js
and add the following configuration:
This requires the raw-loader package, so that will need to be installedas well by running:
Keep in mind that any time an update is made to the next.config.js
configuration file, it’s best to restart theapplication.
Now reopen the post
template and update the PostTemplate
component to handle the frontmatter
data:
Then in the same component, add <p>{content}</p>
for rendering the Markdown content:
You should now see the title: Build an awesome Next.js blogAs well as: We’re building an awesome Next.js blog using Markdown
Finally, the application will need to convert the content we have written in Markdown so it renders to users in theexpected format.
Add the following to awesome-nextjs-blog.md
:
This will display the content as plain text in the browser. To render the content written in Markdown; installreact-markdown by running:
Then in the [slug].js
file, import ReactMarkdown
and update the PostTemplate
component to replace<p>{content}</p>
with <ReactMarkdown source={content} />
.
Collectively it should look like this: Mac browser for flash.
Now you’re all set! If you’re new to Markdown, check out this handy cheat sheet which is great to reference as you’re learning the ropes.
Telmo regularly posts helpful React development tips and guides on Twitter. Be sure to follow him at @telmo
Knex Js
Project Structure⌗
Here’s the project structure for my application. Adjust to your needs.I used the Express application generator to scaffold the program.
Dockerfile & docker-compose⌗
The Dockerfile must have the Postgres connection string as a build argument.
See Docker ARG, ENV and .env - a Complete Guide for more information on ARG, ENV and Docker.
Example excerpt from Dockerfile
:
Example docker-compose.yml
:
Install Knex.js & Postgres⌗
We also use docker-compose.yaml
to create a container for the Postgres database. Let’s call the service db
:
The database folder is located inside the node application: node_app/db
.
Here’s the Dockerfile for the Postgres database:
create.sql
spins up the databases for development, testing, production:
The Postgres connection string inside the docker-compose.yaml
reflects this:
Your Node application must have Knex.js and the postgres driver.
For example:
Initialization⌗
Initialize knex:
Now we can adjust the configuration to our needs: Fdisk for mac.
Migrations⌗
up
and down
functions:You’ll find a new file inside the migrations
folder.
Run migrations:
Seeds⌗
For example:
This will create new folders called seeds/development
and seeds/test
which you can modify.
Knex Cheat Sheet
In my repository, the files are node_app/seeds/development/show-seed.js
and node_app/seeds/tet/show-seed.js
Then run docker-compose exec node_app seed:run --env development
(and --env test
).
Glue Everything Together⌗
Create knex.js
inside the database folder (node_app/db/knex.js
):
Example GitHub Repository⌗
See GitHub.