1
1
const dev = process . env . NODE_ENV !== 'production'
2
- // const goal = process.env.GOAL
3
2
4
3
const { createServer } = require ( 'http' )
5
4
const { parse } = require ( 'url' )
@@ -13,6 +12,7 @@ const glob = require('glob')
13
12
const app = next ( { dev } )
14
13
const handle = app . getRequestHandler ( )
15
14
const route = pathMatch ( )
15
+ const SERVE_PORT = 3001
16
16
17
17
// const moduleAlias = require('module-alias')
18
18
// For the development version, we'll use React.
@@ -24,7 +24,6 @@ const route = pathMatch()
24
24
}
25
25
*/
26
26
27
- // const langMatch = route('/lang/:name')
28
27
mobxReact . useStaticRendering ( true )
29
28
30
29
const supportLanguages = glob
@@ -48,65 +47,44 @@ const getMessages = locale => {
48
47
return messageCache . get ( locale )
49
48
}
50
49
51
- // communities view for root
52
- const communitiesQuery = route ( '/communities' )
53
- const communitiesSubQuery = route ( '/communities/:sub' )
54
- // users view for root
55
- const usersQuery = route ( '/users' )
56
- const usersSubQuery = route ( '/users/:sub' )
57
- // const communityQuery = route('/:main')
58
- const communityQuery = route ( '/:main' )
59
- const communitySubQuery = route ( '/:main/:sub' )
50
+ // routes
51
+ const communitiesQuery = route ( '/communities/:sub?' )
52
+ const usersQuery = route ( '/users/:sub?' )
60
53
const localeQuery = route ( '/locale/:lang' )
54
+ const communityQuery = route ( '/:main/:sub?' )
61
55
62
56
app . prepare ( ) . then ( ( ) => {
63
57
createServer ( ( req , res ) => {
64
- const { pathname } = parse ( req . url )
65
- /* console.log('---------> server parse(req.url): ', parse(req.url)) */
66
- // const homeMatch = homeQuery(pathname)
67
- /* const communitiesMatch = communitiesQuery(pathname) */
68
- /* const communitiesSubMatch = communitiesSubQuery(pathname) */
69
- /* const usersMatch = usersQuery(pathname) */
70
- /* const usersSubMatch = usersSubQuery(pathname) */
71
- /* const localeMatch = localeQuery(pathname) */
58
+ /*
59
+ req.url = req.url.replace(/\/$/, '')
60
+ if (req.url === '') {
61
+ req.url = '/'
62
+ }
63
+ handle(req, res)
64
+ */
72
65
73
- const communityMatch = communityQuery ( pathname )
74
- const communitySubMatch = communitySubQuery ( pathname )
66
+ const { pathname } = parse ( req . url )
75
67
76
68
const accept = accepts ( req )
77
69
const locale = accept . language ( supportLanguages ) // 'zh'
78
70
79
71
if ( localeQuery ( pathname ) ) {
80
72
res . setHeader ( 'Content-Type' , 'application/json;charset=utf-8' )
81
73
return res . end ( JSON . stringify ( getMessages ( localeQuery ( pathname ) . lang ) ) )
82
- } else if ( communitiesQuery ( pathname ) || communitiesSubQuery ( pathname ) ) {
74
+ } else if ( communitiesQuery ( pathname ) ) {
83
75
return app . render ( req , res , '/communities' )
84
- } else if ( usersQuery ( pathname ) || usersSubQuery ( pathname ) ) {
76
+ } else if ( usersQuery ( pathname ) ) {
85
77
return app . render ( req , res , '/users' )
86
- } else if ( communityMatch ) {
87
- return app . render ( req , res , '/' , communityMatch )
88
- } else if ( communitySubMatch ) {
89
- return app . render ( req , res , '/' , communitySubMatch )
78
+ } else if ( communityQuery ( pathname ) ) {
79
+ return app . render ( req , res , '/' )
90
80
}
91
- /*
92
- } else if (communityMatch) {
93
- return app.render(req, res, '/', communityMatch)
94
- } else if (communitySubMatch) {
95
- return app.render(req, res, '/', communitySubMatch)
96
- }
97
- */
98
81
99
- /*
100
- if (homeMatch) {
101
- return app.render(req, res, '/', homeMatch)
102
- }
103
- */
104
82
// now index page go this way
105
83
req . locale = locale
106
84
req . messages = getMessages ( locale )
107
85
108
86
return handle ( req , res )
109
- } ) . listen ( 3001 , err => {
87
+ } ) . listen ( SERVE_PORT , err => {
110
88
if ( err ) throw err
111
89
console . log ( '> Ready on http://localhost:3001' )
112
90
} )
0 commit comments