Skip to content

Commit f33097c

Browse files
codeSTACKrdelbaoliveirasamcx
authored
with-mongodb update to add both App and Pages Router examples (#68461)
Updated all dependencies and added an app route to demonstrate both pages an app directories. --------- Co-authored-by: Delba de Oliveira <32464864+delbaoliveira@users.noreply.github.com> Co-authored-by: Sam Ko <sam@vercel.com>
1 parent 3ae338c commit f33097c

19 files changed

+436
-245
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
MONGODB_URI=
1+
MONGODB_URI="YOUR_MONGODB_CONNECTION_STRING"

examples/with-mongodb/app/actions.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"use server";
2+
3+
import client from "@/lib/mongodb";
4+
5+
export async function testDatabaseConnection() {
6+
let isConnected = false;
7+
try {
8+
const mongoClient = await client.connect();
9+
// Send a ping to confirm a successful connection
10+
await mongoClient.db("admin").command({ ping: 1 });
11+
console.log(
12+
"Pinged your deployment. You successfully connected to MongoDB!",
13+
); // because this is a server action, the console.log will be outputted to your terminal not in the browser
14+
return !isConnected;
15+
} catch (e) {
16+
console.error(e);
17+
return isConnected;
18+
}
19+
}
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
import Image from "next/image";
2+
import { testDatabaseConnection } from "../actions";
3+
import Link from "next/link";
4+
5+
export default async function Home() {
6+
const isConnected = await testDatabaseConnection();
7+
8+
return (
9+
<main className="flex min-h-screen flex-col items-center justify-between p-24">
10+
<div className="z-10 max-w-5xl w-full items-center justify-between font-mono text-sm lg:flex">
11+
<p className="fixed left-0 top-0 flex w-full justify-center border-b border-gray-300 bg-gradient-to-b from-zinc-200 pb-6 pt-8 backdrop-blur-2xl dark:border-neutral-800 dark:bg-zinc-800/30 dark:from-inherit lg:static lg:w-auto lg:rounded-xl lg:border lg:bg-gray-200 lg:p-4 lg:dark:bg-zinc-800/30">
12+
App Router: Get started by editing&nbsp;
13+
<code className="font-mono font-bold">app/app-demo/page.tsx</code>
14+
</p>
15+
<div className="fixed bottom-0 left-0 flex h-48 w-full items-end justify-center bg-gradient-to-t from-white via-white dark:from-black dark:via-black lg:static lg:h-auto lg:w-auto lg:bg-none">
16+
<a
17+
className="pointer-events-none flex place-items-center gap-2 p-8 lg:pointer-events-auto lg:p-0"
18+
href="https://vercel.com?utm_source=create-next-app&utm_medium=appdir-template&utm_campaign=create-next-app"
19+
target="_blank"
20+
rel="noopener noreferrer"
21+
>
22+
By{" "}
23+
<Image
24+
src="/vercel.svg"
25+
alt="Vercel Logo"
26+
className="dark:invert"
27+
width={100}
28+
height={24}
29+
priority
30+
/>
31+
</a>
32+
</div>
33+
</div>
34+
35+
<div className="flex flex-col place-items-center gap-12">
36+
<div className="relative flex place-items-center gap-6 before:absolute before:h-[300px] before:w-[480px] before:-translate-x-1/2 before:rounded-full before:bg-gradient-radial before:from-white before:to-transparent before:blur-2xl before:content-[''] after:absolute after:-z-20 after:h-[180px] after:w-[240px] after:translate-x-1/3 after:bg-gradient-conic after:from-sky-200 after:via-blue-200 after:blur-2xl after:content-[''] before:dark:bg-gradient-to-br before:dark:from-transparent before:dark:to-blue-700 before:dark:opacity-10 after:dark:from-sky-900 after:dark:via-[#0141ff] after:dark:opacity-40 before:lg:h-[360px] z-[-1]">
37+
<Image
38+
className="relative dark:drop-shadow-[0_0_0.3rem_#ffffff70] dark:invert"
39+
src="/next.svg"
40+
alt="Next.js Logo"
41+
width={180}
42+
height={37}
43+
priority
44+
/>
45+
{" + "}
46+
<Image
47+
className="relative dark:drop-shadow-[0_0_0.3rem_#ffffff70] saturate-0 brightness-0 dark:saturate-100 dark:brightness-100"
48+
src="/mongodb.svg"
49+
alt="MongoDB Logo"
50+
width={180}
51+
height={37}
52+
priority
53+
/>
54+
</div>
55+
{isConnected ? (
56+
<h2 className="text-lg text-green-500">
57+
You are connected to MongoDB!
58+
</h2>
59+
) : (
60+
<h2 className="text-lg text-red-500">
61+
You are NOT connected to MongoDB. Check the <code>README.md</code>{" "}
62+
for instructions.
63+
</h2>
64+
)}
65+
<p className="fixed left-0 top-0 flex w-full justify-center border-b border-gray-300 bg-gradient-to-b from-zinc-200 pb-6 pt-8 backdrop-blur-2xl dark:border-neutral-800 dark:bg-zinc-800/30 dark:from-inherit lg:static lg:w-auto lg:rounded-xl lg:border lg:bg-gray-200 lg:p-4 lg:dark:bg-zinc-800/30">
66+
This page uses the&nbsp;<strong>App Router</strong>. Check out the
67+
Pages Router version here:&nbsp;
68+
<Link
69+
href="/"
70+
className="underline transition-colors ease-in-out hover:text-green-500"
71+
>
72+
<code>pages/index.tsx</code>
73+
</Link>
74+
</p>
75+
</div>
76+
77+
<div className="mb-32 grid text-center lg:max-w-5xl lg:w-full lg:mb-0 lg:grid-cols-4 lg:text-left">
78+
<a
79+
href="https://nextjs.org/docs?utm_source=create-next-app&utm_medium=appdir-template&utm_campaign=create-next-app"
80+
className="group rounded-lg border border-transparent px-5 py-4 transition-colors hover:border-gray-300 hover:bg-gray-100 hover:dark:border-neutral-700 hover:dark:bg-neutral-800/30"
81+
target="_blank"
82+
rel="noopener noreferrer"
83+
>
84+
<h2 className={`mb-3 text-2xl font-semibold`}>
85+
Docs{" "}
86+
<span className="inline-block transition-transform group-hover:translate-x-1 motion-reduce:transform-none">
87+
-&gt;
88+
</span>
89+
</h2>
90+
<p className={`m-0 max-w-[30ch] text-sm opacity-50`}>
91+
Find in-depth information about Next.js features and API.
92+
</p>
93+
</a>
94+
95+
<a
96+
href="https://nextjs.org/learn?utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
97+
className="group rounded-lg border border-transparent px-5 py-4 transition-colors hover:border-gray-300 hover:bg-gray-100 hover:dark:border-neutral-700 hover:dark:bg-neutral-800/30"
98+
target="_blank"
99+
rel="noopener noreferrer"
100+
>
101+
<h2 className={`mb-3 text-2xl font-semibold`}>
102+
Learn{" "}
103+
<span className="inline-block transition-transform group-hover:translate-x-1 motion-reduce:transform-none">
104+
-&gt;
105+
</span>
106+
</h2>
107+
<p className={`m-0 max-w-[30ch] text-sm opacity-50`}>
108+
Learn about Next.js in an interactive course with&nbsp;quizzes!
109+
</p>
110+
</a>
111+
112+
<a
113+
href="https://vercel.com/templates?framework=next.js&utm_source=create-next-app&utm_medium=default-template-tw&utm_campaign=create-next-app&database=mongodb"
114+
className="group rounded-lg border border-transparent px-5 py-4 transition-colors hover:border-gray-300 hover:bg-gray-100 hover:dark:border-neutral-700 hover:dark:bg-neutral-800/30"
115+
target="_blank"
116+
rel="noopener noreferrer"
117+
>
118+
<h2 className={`mb-3 text-2xl font-semibold`}>
119+
Templates{" "}
120+
<span className="inline-block transition-transform group-hover:translate-x-1 motion-reduce:transform-none">
121+
-&gt;
122+
</span>
123+
</h2>
124+
<p className={`m-0 max-w-[30ch] text-sm opacity-50`}>
125+
Explore starter templates for Next.js + MongoDB.
126+
</p>
127+
</a>
128+
129+
<a
130+
href="https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.heygears.com%2Fvercel%2Fnext.js%2Ftree%2Fcanary%2Fexamples%2Fwith-mongodb&project-name=nextjs-mongodb&repository-name=nextjs-mongodb&integration-ids=oac_jnzmjqM10gllKmSrG0SGrHOH"
131+
className="group rounded-lg border border-transparent px-5 py-4 transition-colors hover:border-gray-300 hover:bg-gray-100 hover:dark:border-neutral-700 hover:dark:bg-neutral-800/30"
132+
target="_blank"
133+
rel="noopener noreferrer"
134+
>
135+
<h2 className={`mb-3 text-2xl font-semibold`}>
136+
Deploy{" "}
137+
<span className="inline-block transition-transform group-hover:translate-x-1 motion-reduce:transform-none">
138+
-&gt;
139+
</span>
140+
</h2>
141+
<p className={`m-0 max-w-[30ch] text-sm opacity-50`}>
142+
Instantly deploy your Next.js site to a shareable URL with Vercel.
143+
</p>
144+
</a>
145+
</div>
146+
</main>
147+
);
148+
}

examples/with-mongodb/app/favicon.ico

25.3 KB
Binary file not shown.

examples/with-mongodb/app/layout.tsx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import type { Metadata } from "next";
2+
import { Inter } from "next/font/google";
3+
import "@/styles/globals.css";
4+
5+
const inter = Inter({ subsets: ["latin"] });
6+
7+
export const metadata: Metadata = {
8+
title: "Create Next App",
9+
description: "Generated by create next app",
10+
};
11+
12+
export default function RootLayout({
13+
children,
14+
}: {
15+
children: React.ReactNode;
16+
}) {
17+
return (
18+
<html lang="en">
19+
<body className={inter.className}>{children}</body>
20+
</html>
21+
);
22+
}

examples/with-mongodb/lib/mongodb.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
1-
import { MongoClient, ServerApiVersion } from "mongodb";
1+
import { MongoClient } from "mongodb";
22

33
if (!process.env.MONGODB_URI) {
44
throw new Error('Invalid/Missing environment variable: "MONGODB_URI"');
55
}
66

77
const uri = process.env.MONGODB_URI;
8-
const options = {
9-
serverApi: {
10-
version: ServerApiVersion.v1,
11-
strict: true,
12-
deprecationErrors: true,
13-
},
14-
};
8+
const options = { appName: "devrel.template.nextjs" };
159

1610
let client: MongoClient;
1711

@@ -33,4 +27,5 @@ if (process.env.NODE_ENV === "development") {
3327

3428
// Export a module-scoped MongoClient. By doing this in a
3529
// separate module, the client can be shared across functions.
30+
3631
export default client;

examples/with-mongodb/next.config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/** @type {import('next').NextConfig} */
2+
const nextConfig = {};
3+
4+
module.exports = nextConfig;

examples/with-mongodb/package.json

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,18 @@
88
"dependencies": {
99
"mongodb": "^6.5.0",
1010
"next": "latest",
11-
"react": "^18.2.0",
12-
"react-dom": "^18.2.0"
11+
"react": "^18",
12+
"react-dom": "^18"
1313
},
1414
"devDependencies": {
15-
"@types/node": "18.7.5",
16-
"@types/react": "16.9.17",
17-
"typescript": "4.6.3"
15+
"@types/node": "^20",
16+
"@types/react": "^18",
17+
"@types/react-dom": "^18",
18+
"autoprefixer": "^10.0.1",
19+
"eslint": "^8",
20+
"eslint-config-next": "14.0.4",
21+
"postcss": "^8",
22+
"tailwindcss": "^3.3.0",
23+
"typescript": "^5"
1824
}
1925
}

examples/with-mongodb/pages/_app.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import "@/styles/globals.css";
2+
import type { AppProps } from "next/app";
3+
4+
export default function App({ Component, pageProps }: AppProps) {
5+
return <Component {...pageProps} />;
6+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Html, Head, Main, NextScript } from "next/document";
2+
3+
export default function Document() {
4+
return (
5+
<Html lang="en">
6+
<Head />
7+
<body>
8+
<Main />
9+
<NextScript />
10+
</body>
11+
</Html>
12+
);
13+
}

0 commit comments

Comments
 (0)