-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.js
55 lines (43 loc) · 1.49 KB
/
middleware.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
// import { NextResponse } from 'next/server';
// import mongoose from 'mongoose';
// // Not Required
// // This function can be marked `async` if using `await` inside
// export function middleware(request) {
// return NextResponse.redirect(new URL('/', request.url));
// }
// // See "Matching Paths" below to learn more
// export const config = {
// matcher: '/afrojack',
// };
// mongoose.connect(process.env.MONGO_URI);
// const db = mongoose.connection;
// db.on('error', (error) => {
// console.error('MongoDB connection error:', error);
// });
// db.once('open', () => {
// console.log('Connected to MongoDB & The Goal is to Reach The PU');
// });
// export default db;
import { NextResponse } from 'next/server';
import { MongoClient } from 'mongodb';
// Not Required
// This function can be marked `async` if using `await` inside
export function middleware(request) {
return NextResponse.redirect(new URL('/', request.url));
}
// See "Matching Paths" below to learn more
export const config = {
matcher: '/afrojack',
};
const client = new MongoClient(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true });
async function connectToDatabase() {
try {
await client.connect();
console.log('Connected to MongoDB & The Goal is to Reach The PU');
return client.db(); // Return the database instance
} catch (error) {
console.error('MongoDB connection error:', error);
}
}
const db = connectToDatabase();
export default db;