-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dbe5d1c
commit cc30776
Showing
6 changed files
with
44 additions
and
198 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,10 @@ | ||
//next-i18next.config.js | ||
|
||
module.exports = { | ||
// debug: true, | ||
i18n: { | ||
defaultLocale: 'zh', | ||
locales: ['zh', 'en'], | ||
}, | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
localePath: require('path').resolve('./public/locales'), // 指定翻译文件的路径 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,37 @@ | ||
/* eslint-disable @typescript-eslint/no-var-requires */ | ||
const express = require('express'); | ||
const next = require('next'); | ||
|
||
const dev = process.env.NEXT_PUBLIC_ENV !== 'production'; | ||
const app = next({ dev }); | ||
const handle = app.getRequestHandler(); | ||
|
||
const { parse } = require('url'); | ||
|
||
const express = require('express'); | ||
const expressRouter = express.Router(); | ||
const server = express(); | ||
// this is the logger for the server | ||
var logger = require('pino-http')(); | ||
const PORT = process.env.PORT || 3000; | ||
|
||
const NODE_PORT = process.env.NODE_PORT | 3000; | ||
async function startServer() { | ||
try { | ||
await app.prepare(); | ||
const server = express(); | ||
|
||
app.prepare().then(() => { | ||
expressRouter.get('*', (req, res) => { | ||
// 页面路由 | ||
logger(req, res); | ||
const parsedUrl = parse(req.url, true); | ||
const { pathname, query } = parsedUrl; | ||
if (pathname.length > 1 && pathname.endsWith('/')) { | ||
return app.render(req, res, pathname.slice(0, -1), query); | ||
} else { | ||
return app.render(req, res, pathname, query); | ||
} | ||
}); | ||
server.use('/', expressRouter); | ||
// 中间件处理尾部斜杠但不重定向 | ||
server.use((req, res, next) => { | ||
if (req.url.length > 1 && req.url.endsWith('/')) { | ||
req.url = req.url.slice(0, -1); | ||
} | ||
next(); | ||
}); | ||
|
||
server.all('*', (req, res) => { | ||
// Be sure to pass `true` as the second argument to `url.parse`. | ||
// This tells it to parse the query portion of the URL. | ||
const parsedUrl = parse(req.url, true); | ||
// 处理所有其他请求 | ||
server.all('*', (req, res) => handle(req, res)); | ||
|
||
return handle(req, res, parsedUrl); | ||
}); | ||
server.listen(PORT, (err) => { | ||
if (err) throw err; | ||
console.log(`> Ready on http://localhost:${PORT}`); | ||
}); | ||
} catch (error) { | ||
console.error('Error starting server:', error); | ||
process.exit(1); | ||
} | ||
} | ||
|
||
server.listen(NODE_PORT, () => | ||
console.log('App listening on port ' + NODE_PORT) | ||
); | ||
}); | ||
startServer(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.