-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
74 lines (66 loc) · 2.06 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
const express = require('express');
const next = require('next');
const path = require('path');
const dev = process.env.NODE_ENV !== 'production';
const app = next({ dev });
const handle = app.getRequestHandler();
const dotenv = require('dotenv');
const mongoose = require('mongoose');
const cookieParser = require('cookie-parser');
const passport = require('passport');
const { parse } = require('url');
const { join } = require('path');
const compression = require('compression');
const routes = require('./server/routes');
const sitemapAndRobots = require('./server/utils/sitemapAndRobots');
dotenv.config();
const options = {
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false,
};
mongoose
.connect(process.env.MONGO_URI, options)
.then(() => console.log('sucess connect database')) // eslint-disable-line no-console
.catch((err) => console.log(err)); // eslint-disable-line no-console
app
.prepare()
.then(() => {
const server = express();
server.use(compression());
server.use(
'/_next',
express.static(path.join(__dirname, '.next'), {
maxAge: '31536000',
}),
);
server.use(
'/static',
express.static(path.join(__dirname, '/static'), {
maxAge: '31536000',
}),
);
server.get('/service-worker.js', (req, res) => {
const parsedUrl = parse(req.url, true);
const { pathname } = parsedUrl;
if (pathname === '/service-worker.js') {
const filePath = join(__dirname, '.next', pathname);
app.serveStatic(req, res, filePath);
} else {
handle(req, res, parsedUrl);
}
});
server.use(cookieParser('nextjs-node-rest-api-startkit'));
server.use(passport.initialize());
sitemapAndRobots({ server });
server.use('/', routes);
server.get('*', (req, res) => handle(req, res));
server.listen(3000, (err) => {
if (err) throw err;
console.log('> Ready on http://localhost:3000'); // eslint-disable-line
});
})
.catch((ex) => {
console.error(ex.stack); // eslint-disable-line
process.exit(1);
});