📅  最后修改于: 2023-12-03 15:03:16.430000             🧑  作者: Mango
normalizePort
函数可以接受一个端口号参数并返回一个有效的端口号;如果端口号无效,则返回 false。
normalizePort(port: string): number | boolean
port
:必选参数,要被规范化的端口号function normalizePort(port) {
const portNumber = parseInt(port, 10);
if (isNaN(portNumber)) {
return port; // named pipe
}
if (portNumber >= 0) {
return portNumber; // portNumber
}
return false;
}
const port = normalizePort(process.env.PORT || '3000');
const express = require('express');
const app = express();
const http = require('http');
const normalizePort = (value) => {
const port = parseInt(value, 10);
if (isNaN(port)) return value;
if (port >= 0) return port;
return false;
};
const port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
const server = http.createServer(app);
server.listen(port, () => {
console.log(`Server listening on port ${port}`);
});
在上面的示例中,normalizePort
函数用于规范化端口号,并且将其作为应用程序的端口号设置。