Skip to content

Commit f6e6cf9

Browse files
committed
Initial commit from Create Next App
0 parents  commit f6e6cf9

File tree

9 files changed

+1411
-0
lines changed

9 files changed

+1411
-0
lines changed

.env.local.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
MONGODB_URI=

.gitignore

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
8+
# testing
9+
/coverage
10+
11+
# next.js
12+
/.next/
13+
/out/
14+
15+
# production
16+
/build
17+
18+
# misc
19+
.DS_Store
20+
*.pem
21+
22+
# debug
23+
npm-debug.log*
24+
yarn-debug.log*
25+
yarn-error.log*
26+
.pnpm-debug.log*
27+
28+
# local env files
29+
.env*.local
30+
31+
# vercel
32+
.vercel
33+
34+
# typescript
35+
*.tsbuildinfo
36+
next-env.d.ts

README.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
## Example app using MongoDB
2+
3+
[MongoDB](https://www.mongodb.com/) is a general purpose, document-based, distributed database built for modern application developers and for the cloud era. This example will show you how to connect to and use MongoDB as your backend for your Next.js app.
4+
5+
If you want to learn more about MongoDB, visit the following pages:
6+
7+
- [MongoDB Atlas](https://mongodb.com/atlas)
8+
- [MongoDB Documentation](https://docs.mongodb.com/)
9+
10+
## Deploy your own
11+
12+
Once you have access to the environment variables you'll need, deploy the example using [Vercel](https://vercel.com?utm_source=github&utm_medium=readme&utm_campaign=next-example):
13+
14+
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?project-name=with-mongodb&repository-name=with-mongodb&repository-url=https%3A%2F%2Fgithub.com%2Fvercel%2Fnext.js%2Ftree%2Fcanary%2Fexamples%2Fwith-mongodb&integration-ids=oac_jnzmjqM10gllKmSrG0SGrHOH)
15+
16+
## How to use
17+
18+
Execute [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app) with [npm](https://docs.npmjs.com/cli/init), [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/), or [pnpm](https://pnpm.io) to bootstrap the example:
19+
20+
```bash
21+
npx create-next-app --example with-mongodb with-mongodb-app
22+
```
23+
24+
```bash
25+
yarn create next-app --example with-mongodb with-mongodb-app
26+
```
27+
28+
```bash
29+
pnpm create next-app --example with-mongodb with-mongodb-app
30+
```
31+
32+
## Configuration
33+
34+
### Set up a MongoDB database
35+
36+
Set up a MongoDB database either locally or with [MongoDB Atlas for free](https://mongodb.com/atlas).
37+
38+
### Set up environment variables
39+
40+
Copy the `env.local.example` file in this directory to `.env.local` (which will be ignored by Git):
41+
42+
```bash
43+
cp .env.local.example .env.local
44+
```
45+
46+
Set each variable on `.env.local`:
47+
48+
- `MONGODB_URI` - Your MongoDB connection string. If you are using [MongoDB Atlas](https://mongodb.com/atlas) you can find this by clicking the "Connect" button for your cluster.
49+
50+
### Run Next.js in development mode
51+
52+
```bash
53+
npm install
54+
npm run dev
55+
56+
# or
57+
58+
yarn install
59+
yarn dev
60+
```
61+
62+
Your app should be up and running on [http://localhost:3000](http://localhost:3000)! If it doesn't work, post on [GitHub discussions](https://github.com/vercel/next.js/discussions).
63+
64+
You will either see a message stating "You are connected to MongoDB" or "You are NOT connected to MongoDB". Ensure that you have provided the correct `MONGODB_URI` environment variable.
65+
66+
When you are successfully connected, you can refer to the [MongoDB Node.js Driver docs](https://mongodb.github.io/node-mongodb-native/3.4/tutorials/collections/) for further instructions on how to query your database.
67+
68+
## Deploy on Vercel
69+
70+
You can deploy this app to the cloud with [Vercel](https://vercel.com?utm_source=github&utm_medium=readme&utm_campaign=next-example) ([Documentation](https://nextjs.org/docs/deployment)).
71+
72+
#### Deploy Your Local Project
73+
74+
To deploy your local project to Vercel, push it to GitHub/GitLab/Bitbucket and [import to Vercel](https://vercel.com/new?utm_source=github&utm_medium=readme&utm_campaign=next-example).
75+
76+
**Important**: When you import your project on Vercel, make sure to click on **Environment Variables** and set them to match your `.env.local` file.
77+
78+
#### Deploy from Our Template
79+
80+
Alternatively, you can deploy using our template by clicking on the Deploy button below.
81+
82+
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?project-name=with-mongodb&repository-name=with-mongodb&repository-url=https%3A%2F%2Fgithub.com%2Fvercel%2Fnext.js%2Ftree%2Fcanary%2Fexamples%2Fwith-mongodb&integration-ids=oac_jnzmjqM10gllKmSrG0SGrHOH)

lib/mongodb.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { MongoClient } from 'mongodb'
2+
3+
const uri = process.env.MONGODB_URI
4+
const options = {}
5+
6+
let client
7+
let clientPromise
8+
9+
if (!process.env.MONGODB_URI) {
10+
throw new Error('Please add your Mongo URI to .env.local')
11+
}
12+
13+
if (process.env.NODE_ENV === 'development') {
14+
// In development mode, use a global variable so that the value
15+
// is preserved across module reloads caused by HMR (Hot Module Replacement).
16+
if (!global._mongoClientPromise) {
17+
client = new MongoClient(uri, options)
18+
global._mongoClientPromise = client.connect()
19+
}
20+
clientPromise = global._mongoClientPromise
21+
} else {
22+
// In production mode, it's best to not use a global variable.
23+
client = new MongoClient(uri, options)
24+
clientPromise = client.connect()
25+
}
26+
27+
// Export a module-scoped MongoClient promise. By doing this in a
28+
// separate module, the client can be shared across functions.
29+
export default clientPromise

0 commit comments

Comments
 (0)