11const dev = process . env . NODE_ENV !== 'production'
2- // const goal = process.env.GOAL
32
43const { createServer } = require ( 'http' )
54const { parse } = require ( 'url' )
@@ -13,6 +12,7 @@ const glob = require('glob')
1312const app = next ( { dev } )
1413const handle = app . getRequestHandler ( )
1514const route = pathMatch ( )
15+ const SERVE_PORT = 3001
1616
1717// const moduleAlias = require('module-alias')
1818// For the development version, we'll use React.
@@ -24,7 +24,6 @@ const route = pathMatch()
2424 }
2525 */
2626
27- // const langMatch = route('/lang/:name')
2827mobxReact . useStaticRendering ( true )
2928
3029const supportLanguages = glob
@@ -48,65 +47,44 @@ const getMessages = locale => {
4847 return messageCache . get ( locale )
4948}
5049
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?' )
6053const localeQuery = route ( '/locale/:lang' )
54+ const communityQuery = route ( '/:main/:sub?' )
6155
6256app . prepare ( ) . then ( ( ) => {
6357 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+ */
7265
73- const communityMatch = communityQuery ( pathname )
74- const communitySubMatch = communitySubQuery ( pathname )
66+ const { pathname } = parse ( req . url )
7567
7668 const accept = accepts ( req )
7769 const locale = accept . language ( supportLanguages ) // 'zh'
7870
7971 if ( localeQuery ( pathname ) ) {
8072 res . setHeader ( 'Content-Type' , 'application/json;charset=utf-8' )
8173 return res . end ( JSON . stringify ( getMessages ( localeQuery ( pathname ) . lang ) ) )
82- } else if ( communitiesQuery ( pathname ) || communitiesSubQuery ( pathname ) ) {
74+ } else if ( communitiesQuery ( pathname ) ) {
8375 return app . render ( req , res , '/communities' )
84- } else if ( usersQuery ( pathname ) || usersSubQuery ( pathname ) ) {
76+ } else if ( usersQuery ( pathname ) ) {
8577 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 , '/' )
9080 }
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- */
9881
99- /*
100- if (homeMatch) {
101- return app.render(req, res, '/', homeMatch)
102- }
103- */
10482 // now index page go this way
10583 req . locale = locale
10684 req . messages = getMessages ( locale )
10785
10886 return handle ( req , res )
109- } ) . listen ( 3001 , err => {
87+ } ) . listen ( SERVE_PORT , err => {
11088 if ( err ) throw err
11189 console . log ( '> Ready on http://localhost:3001' )
11290 } )
0 commit comments