forked from jimmylee/next-postgres-sequelize
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
68 lines (56 loc) · 2.05 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
import express from 'express';
import next from 'next';
import http from 'http';
import cors from 'cors';
import morgan from 'morgan';
import url from 'url';
import compression from 'compression';
import bodyParser from 'body-parser';
import cookieParser from 'cookie-parser';
import passport from 'passport';
import setupAuth from './api/auth';
import setupApi from './api';
import queries from './api/controllers/queries';
import { User, Comment, Post } from './api/models';
const dev = process.env.NODE_ENV !== 'production';
const port = parseInt(process.env.PORT, 10) || 8000;
const app = next({ dev, quiet: false });
const nextRequestHandler = app.getRequestHandler();
app.prepare().then(() => {
const server = express();
if (!dev) {
server.use(compression());
}
server.use('/static', express.static(__dirname + '/static'));
server.use(cookieParser());
server.use(morgan('dev'));
server.use(cors({ credentials: true, origin: true }));
server.use(bodyParser.json());
server.use(
bodyParser.urlencoded({
extended: false,
})
);
setupAuth(server, passport);
setupApi(server);
server.get('/post/:id', async (req, res) => {
const users = await User.findAll(queries.users.list({ req, User, Post, Comment }));
const posts = await Post.findAll(queries.posts.list({ User, Post, Comment }));
const comments = await Comment.findAll(queries.comments.list({ User, Post, Comment }));
const params = { id: req.params.id, users, comments, posts };
return app.render(req, res, '/post', params);
});
server.get('*', async (req, res) => {
const users = await User.findAll(queries.users.list({ req, User, Post, Comment }));
const posts = await Post.findAll(queries.posts.list({ User, Post, Comment }));
const comments = await Comment.findAll(queries.comments.list({ User, Post, Comment }));
const params = { users, comments, posts };
return app.render(req, res, req.url, params);
});
server.listen(port, err => {
if (err) {
throw err;
}
console.log(`Running on localhost:${port}`);
});
});