这是一个例子定制服务器快递 https://github.com/zeit/next.js/blob/85769c3d3296cdcddc0fb36f05058c8e451ca57f/examples/custom-server-express/他们经过的地方id
从服务器端到客户端 https://github.com/zeit/next.js/blob/85769c3d3296cdcddc0fb36f05058c8e451ca57f/examples/custom-server-express/server.js#L21-L23
所以在你的情况下它会是这样的
服务器.js
const { createServer } = require('http');
const { parse } = require('url');
const next = require('next');
const port = parseInt(process.env.PORT, 10) || 3000;
const dev = process.env.NODE_ENV !== 'production';
const app = next({ dev });
const handle = app.getRequestHandler();
app.prepare().then(() => {
createServer((req, res) => {
const parsedUrl = parse(req.url, true);
const { pathname, query } = parsedUrl;
if (pathname === '/pl/index') {
app.render(req, res, '/index', { ...query, lang: 'pl' });
} else if (pathname === '/en/index') {
app.render(req, res, '/index', { ...query, lang: 'en' });
} else {
handle(req, res, parsedUrl);
}
}).listen(port, err => {
if (err) throw err;
console.log(`> Ready on http://localhost:${port}`);
});
});
页面/index.js
import React from 'react';
import { withRouter } from 'next/router';
function IndexPage(props) {
return <h1>index lang: {props.router.query.lang}</h1>;
}
export default withRouter(IndexPage);
即将/pl/index
将渲染index lang: pl
and
即将/en/index
将渲染index lang: en
因此
希望这可以帮助!