您可以在服务器上创建一个本地主机并将请求重定向到该本地主机。
为此,请在主文件夹中创建一个“server.js”文件。
在该文件夹内创建一个在生产模式下运行的进程(在端口 4000 上)。
服务器.js:
const { createServer } = require('http');
const { parse } = require('url');
const next = require('next');
const dev = process.env.NODE_ENV !== 'production';
const hostname = 'localhost';
const port = process.env.PORT || 4000;
// when using middleware `hostname` and `port` must be provided below
const app = next({ dev, hostname, port });
const handle = app.getRequestHandler();
app.prepare().then(() => {
createServer(async (req, res) => {
try {
// 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);
const { pathname, query } = parsedUrl;
if (pathname === '/a') {
await app.render(req, res, '/a', query);
} else if (pathname === '/b') {
await app.render(req, res, '/b', query);
} else {
await handle(req, res, parsedUrl);
}
} catch (err) {
console.error('Error occurred handling', req.url, err);
res.statusCode = 500;
res.end('internal server error');
}
}).listen(port, (err) => {
if (err) throw err;
console.log(`> Ready on http://${hostname}:${port}`);
});
});
之后制作一个“service.js”文件,将所有网址重定向到本地主机(反向代理)
网络配置:
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="ReverseProxyInboundRule1" stopProcessing="true">
<match url="(.*)" />
<action type="Rewrite" url="http://localhost:4000/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
最后创建“service.js”文件,将 server.js 文件作为服务运行。
服务.js:
var Service = require('node-windows').Service;
var path = require('path');
var svc = new Service({
name: 'service name',
description: 'example server',
script: path.join(__dirname, 'server.js'),
workingdirectory: path.join(__dirname, 'server.js'),
nodeOptions: [],
env: {
name: 'NODE_ENV',
value: 'production',
},
});
//svc.uninstall();
svc.on('install', function () {
svc.start();
});
svc.install();
现在使用“node service.js”命令运行service.js。
现在您可以在服务器的服务中看到“服务名称”服务(您可以检查它是否没有启动,手动启动它)
- 要卸载服务,您可以取消注释“svc.uninstall();” service.js 文件中的行以及有关安装和启动服务的注释行