-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
98 lines (80 loc) · 3.2 KB
/
index.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
const next = require('next');
const nextAuth = require('next-auth');
const nextAuthConfig = require('./next-auth.config');
const admin = require('./routes/admin');
const account = require('./routes/account');
const requests = require('./routes/requests');
const customRoutes = require('./custom-routes');
const routes = {
admin,
account,
requests,
};
// Load environment variables from .env file if present
require('dotenv').load();
process.on('uncaughtException', (err) => {
// eslint-disable-next-line no-console
console.error('Uncaught Exception: ', err);
});
process.on('unhandledRejection', (reason, p) => {
// eslint-disable-next-line no-console
console.error('Unhandled Rejection: Promise:', p, 'Reason:', reason);
});
// Default when run with `npm start` is 'production' and default port is '80'
// `npm run dev` defaults mode to 'development' & port to '3000'
process.env.NODE_ENV = process.env.NODE_ENV || 'production';
process.env.PORT = process.env.PORT || 80;
// Initialize Next.js
const nextApp = next({
dir: '.',
dev: (process.env.NODE_ENV === 'development'),
});
const customRoutesHandler = customRoutes.getRequestHandler(nextApp);
// Add next-auth to next app
nextApp
.prepare()
// Load configuration and return config object
.then(nextAuthConfig)
// Pass Next.js App instance and NextAuth options to NextAuth
// Note We do not pass a port in nextAuthOptions, because we want to add some
// additional routes before Express starts (if you do pass a port, NextAuth
// tells NextApp to handle default routing and starts Express automatically).
.then(nextAuthOptions => nextAuth(nextApp, nextAuthOptions))
.then((nextAuthOptions) => {
// Get Express and instance of Express from NextAuth
const { express } = nextAuthOptions;
const { expressApp } = nextAuthOptions;
// Add admin routes
routes.admin(expressApp);
// Add account management route - reuses functions defined for NextAuth
routes.account(expressApp, nextAuthOptions.functions);
// Add request routes
routes.requests(expressApp);
// Serve fonts from ionicon npm module
expressApp.use('/fonts/ionicons', express.static('./node_modules/ionicons/dist/fonts'));
// A simple example of custom routing
// Send requests for '/custom-route/{anything}' to 'pages/examples/routing.js'
// Note: To make capturing a slug easier when rendering both client
// and server side, name it ':id'
expressApp.get('/custom-route/:id', (req, res) => nextApp
.render(req, res, '/examples/routing', req.params));
expressApp.use(customRoutesHandler);
// Default catch-all handler to allow Next.js to handle all other routes
expressApp.all('*', (req, res) => {
const nextRequestHandler = nextApp.getRequestHandler();
return nextRequestHandler(req, res);
});
expressApp.listen(process.env.PORT, (err) => {
if (err) {
throw err;
}
// eslint-disable-next-line no-console
console.log(`> Ready on http://localhost:${process.env.PORT} [${process.env.NODE_ENV}]`);
});
})
.catch((err) => {
/* eslint-disable no-console */
console.log('An error occurred, unable to start the server');
console.log(err);
/* eslint-enable */
});