Skip to content

Commit

Permalink
fix: server run bug
Browse files Browse the repository at this point in the history
  • Loading branch information
521xueweihan committed Aug 8, 2024
1 parent dbe5d1c commit cc30776
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 198 deletions.
2 changes: 0 additions & 2 deletions next-i18next.config.js
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'), // 指定翻译文件的路径
};
8 changes: 3 additions & 5 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ const { i18n } = require('./next-i18next.config');

module.exports = withBundleAnalyzer({
i18n,
reactStrictMode: true,
compress: true, // 启用压缩
eslint: {
dirs: ['src'],
},

reactStrictMode: false,

// Uncoment to add domain whitelist
images: {
domains: [
Expand Down Expand Up @@ -43,11 +42,10 @@ module.exports = withBundleAnalyzer({
test: /\.md$/,
use: 'raw-loader',
});

return config;
},

// experimental: {
// disableOptimizedLoading: true,
// scrollRestoration: true,
// },
});
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
"next": "^12.1.6",
"next-i18next": "^15.3.0",
"nprogress": "^0.2.0",
"pino-http": "^8.2.1",
"raw-loader": "^4.0.2",
"rc-image": "^7.0.0-2",
"rc-util": "^5.34.1",
Expand Down
57 changes: 26 additions & 31 deletions server.js
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();
2 changes: 1 addition & 1 deletion src/pages/repository/[rid]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const RepositoryPage: NextPage<Props> = ({ repo }) => {
t={t}
i18n_lang={i18n.language}
/>
<div className='h-8 lg:h-36'></div>
<div className='h-8 lg:h-36' />
</>
);
};
Expand Down
Loading

0 comments on commit cc30776

Please sign in to comment.