Simply copy and paste the following command line in your terminal to create your first Strapi project.
npx create-strapi-app
my-project
Updated April 2023 This tutorial will walk you through how to use Next.js to power your UI, complete using GraphQL, Stripe, Strapi, and Next to developing a full-stack application complete with the powerful Strapi (Headless CMS) powering the backend.
Get ready to develop an online food ordering app, more info on the tech stack here: Next.js, GraphQL, Stripe and Strapi!
From sign up to order, you are going to let users discover restaurants, dishes and order meals.
Your app will be complete with user login, registration, authentication, image upload, restaurant creation, dish creation, cart functionality, and stripe order integration.
The demo of the final result should make you hungry:
Note: the source code is available on GitHub here
Screenshots of final product:
Strapi is the most advanced open-source Node.js Headless Content Management System used to build scalable, secure, production-ready APIs quickly and efficiently saving developers countless hours of development.
With its extensible plugin system, it provides an enormous set of built-in features: Admin Panel, Authentication & Permissions management, Content Management, API Generator, etc. Strapi is 100% open-source, which means:
Next is a lightweight React framework to create server-rendered applications. Next.js will take care of the heavy lifting of the application build such as code splitting, HMR (hot module replacement) SSR (server-side rendering) and allow us to focus on writing the code, not our build config.
GraphQL is a query language also developed by Facebook to allow the front end of an application to easily query an application's API. Each query requests only the data needed to be rendered by the current view. This allows the developer to craft a great user experience across multiple devices and screen sizes.
Stripe is one payment processor for applications today. Stripe has developed the tools and SDKs to allow developers to craft and integrate secure, compliant payment processing into any app with ease.
To set up Next.js you will need an empty directory to install the dependencies and host our project root.
This project will be split into two parts, one for the front end (Next.js code) and one for the backend (Strapi code).
mkdir next-food-delivery
next-food-delivery
folder in your favourite code editor, VS Code preferably and run the following code in the integrated terminal. npx create-next-app@latest frontend
You should see the following output.
➜ npx create-next-app@latest frontend
Need to install the following packages:
create-next-app@13.3.4
Ok to proceed? (y) y
Here are the options I selected for this tutorial, make sure you chose the same ones to follow along:
➜ next-food-delivery npx create-frontend frontend
Need to install the following packages:
create-next-app@13.3.4
Ok to proceed? (y) y
✔ Would you like to use TypeScript with this project? … No
✔ Would you like to use ESLint with this project? … Yes
✔ Would you like to use `src/` directory with this project? … No
✔ Would you like to use experimental `app/` directory with this project? … No
✔ What import alias would you like configured? … @/*
Creating a new Next.js app
The following code will create a folder named frontend
and set up NextJS.
First things first, let's set up our environment variables to avoid hard coding the API URL and having to update it on every deployment. Create a new file in the project’s directory and add the following code.
cd frontend
touch .env.development
Inside /.env.development
development file add the following code:
NEXT_PUBLIC_API_URL='http://localhost:1337'
This tutorial makes use of Next And Tailwind to implement Tailwind into the application.
Tailwind is a front-end library to easily style your application. This will take care of the heavy lifting on the front end.
Open your terminal in the frontend
directory and run the following command:
Note: if you selected to use tailwind in the previous option you can omit this step.
npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init -p
Configure your template paths
Add the following code inside the tailwind.config.js
file.
1
2
3
4
5
6
7
8
9
10
11
12
/** @type {import('tailwindcss').Config} */
module.exports = {
content: [
"./app/**/*.{js,ts,jsx,tsx}",
"./pages/**/*.{js,ts,jsx,tsx}",
"./components/**/*.{js,ts,jsx,tsx}",
],
theme: {
extend: {},
},
plugins: [],
};
Add the Tailwind directives to your CSS
Add the @tailwind directives by replacing your css inside the styles/globals.css
file with the following.
1
2
3
@tailwind base;
@tailwind components;
@tailwind utilities;
If the above was created for you automatically, you can remove all of the other css after the @tailwind directives.
We are importing our CSS in the _app.js
file inside the pages directory . This will allow us to share our CSS and and Layout component across all pages.
You can read more about the
_app.js
handling here
_app.js
to see where we are importing our CSS.Path: /frontend/pages/_app.js
1
2
3
4
5
import "@/styles/globals.css";
export default function App({ Component, pageProps }) {
return <Component {...pageProps} />;
}
Path: frontend/pages/index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import Head from "next/head";
export default function Home() {
return (
<>
<Head>
<title>Create Next App</title>
<meta name="description" content="Generated by create next app" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="icon" href="/favicon.ico" />
</Head>
<main className="mx-auto container">
<button className="inline-block py-3 px-7 w-full md:w-auto text-lg leading-7 text-green-50 bg-green-500 hover:bg-green-600 font-medium text-center focus:ring-2 focus:ring-green-500 focus:ring-opacity-50 border border-transparent rounded-md shadow-sm">
Primary button
</button>
</main>
</>
);
}
npm run dev
Open this URL, localhost:3000, in your favourite browser to view the next app. You should get an output similar to the one below.
Designing the page Now that we have Tailwind running inside of our Next project, we can style the shared frontend components like the nav bar.
Add the following code bellow:
Path: /frontend/components/Layout.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import Head from "next/head";
import Link from "next/link";
function Navigation() {
return (
<nav className="container mx-auto flex justify-between p-6 px-4">
<div className="flex justify-between items-center w-full">
<div className="xl:w-1/3">
<Link
className="block text-lg max-w-max ext-coolGray-500 hover:text-coolGray-900 font-medium"
href="/"
>
Food Order App
</Link>
</div>
<div className="xl:block xl:w-1/3">
<div className="flex items-center justify-end">
<Link
className="text-coolGray-500 hover:text-coolGray-900 font-medium"
href="/"
>
Home
</Link>
<Link
className="inline-block py-2 px-4 mr-2 leading-5 text-coolGray-500 hover:text-coolGray-900 bg-transparent font-medium rounded-md"
href="/login"
>
Log In
</Link>
<Link
className="inline-block py-2 px-4 text-sm leading-5 text-green-50 bg-green-500 hover:bg-green-600 font-medium focus:ring-2 focus:ring-green-500 focus:ring-opacity-50 rounded-md"
href="/register"
>
Sign Up
</Link>
</div>
</div>
</div>
</nav>
);
}
export default function Layout(props) {
const title = "Welcome to Nextjs";
return (
<div>
<Head>
<title>{title}</title>
<meta charSet="utf-8" />
<meta name="viewport" content="initial-scale=1.0, width=device-width" />
</Head>
<Navigation />
<div className="container mx-auto px-4">{props.children}</div>
</div>
);
}
_app.js
file to use the new Layout component across the application:Path: /frontend/frontend/pages/_app.js
1
2
3
4
5
6
7
8
9
10
import "@/styles/globals.css";
import Layout from "@/components/Layout";
export default function App({ Component, pageProps }) {
return (
<Layout>
<Component {...pageProps} />
</Layout>
);
}
You should now have a shared header bar across all your pages, as shown in the output below:
Path: /frontend/pages/register.js
1
2
3
4
/* pages/register.js */
export default function RegisterRoute() {
return <h1>Sign Up</h1>;
}
Path: /frontend/pages/login.js
1
2
3
4
5
/* pages/login.js */
export default function LoginRoute() {
return <h1>Log In</h1>;
}
You should now see the routes at http://localhost:3000/login and http://localhost:3000/register
Setting up the database This tutorial uses PostgreSQL as the database for this application.
This tutorial uses Windows 10 as its operating system.
PgAdmin is installed when you install PostgreSQL.
When PgAdmin opens, you will be prompted to put in the password entered during the installation as shown below.
On the left navigation bar, click on Servers and click on PostgreSQL 14.
Right-click on Databases, hover over Create and click on Database.
You can name the database anything you desire, but, in this tutorial, the name of the database is nextapp. Once you're done naming the database, hit save.
The name of the database, nextapp, will be shown on the left navigation bar. Clicking on it will prompt a drop-down as shown below:
Having a frontend is good, but your app needs a backend to manage users, restaurants, dishes and orders. To make the magic happen, let's create a Strapi API.
Please use version >=Node 9 and have PostgreSQL installed and running on your machine.
Create Strapi server
next-food-delivery
's directory and run the following code to create a Strapi application. npx create-strapi-app@latest backend
Here are my options:
➜ next-food-delivery npx create-strapi-app@latest backend
? Choose your installation type Custom (manual settings)
? Choose your preferred language JavaScript
? Choose your default database client postgres
? Database name: nextapp
? Host: 127.0.0.1
? Port: 5432
? Username: postgres
? Password: ***********
? Enable SSL connection: No
Creating a project with custom database options.
Available commands in your project:
yarn develop
Start Strapi in watch mode. (Changes in Strapi project files will trigger a server restart)
yarn start
Start Strapi without watch mode.
yarn build
Build Strapi admin panel.
yarn strapi
Display all available commands.
You can start by doing:
You can start your project by running the following command:
cd backend
npm run develop
You should see the following code that was automatically setup for you when we created your Strapi project with your postgres credentials.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//
postgres: {
connection: {
connectionString: env('DATABASE_URL'),
host: env('DATABASE_HOST', 'localhost'),
port: env.int('DATABASE_PORT', 5432),
database: env('DATABASE_NAME', 'strapi'),
user: env('DATABASE_USERNAME', 'strapi'),
password: env('DATABASE_PASSWORD', 'strapi'),
ssl: env.bool('DATABASE_SSL', false) && {
key: env('DATABASE_SSL_KEY', undefined),
cert: env('DATABASE_SSL_CERT', undefined),
ca: env('DATABASE_SSL_CA', undefined),
capath: env('DATABASE_SSL_CAPATH', undefined),
cipher: env('DATABASE_SSL_CIPHER', undefined),
rejectUnauthorized: env.bool(
'DATABASE_SSL_REJECT_UNAUTHORIZED',
true
),
},
schema: env('DATABASE_SCHEMA', 'public'),
},
pool: { min: env.int('DATABASE_POOL_MIN', 2), max: env.int('DATABASE_POOL_MAX', 10) },
},
//
You can find your database variables within your .env file.
Good job, you successfully set up both Next.js and Strapi projects! 🎉
🏠 In the next section, you will learn how to display the list of restaurants: https://strapi.io/blog/nextjs-react-hooks-strapi-restaurants-2.
Ryan is an active member of the Strapi community and he's been contributing at a very early stage by writing awesome tutorial series to help fellow Strapier grow and learn.