这是我的最小代码示例:
...
const url = typeof process.env.url === 'string' ? process.env.url : {do not start a server}
...
server.start(options, ({ port }) => console.log(`Server is running on http://localhost:${port}`));
如果未设置process.env.url
(请参阅代码示例),如何抛出错误(或仅打印出一些内容)并避免启动服务器。
答案 0 :(得分:0)
您可以简单地抛出错误并退出该过程:
function notValid() {
throw new Error('The passed url is not valid!');
process.exit()
}
const url = typeof process.env.url === 'string' ? process.env.url : notValid();
server.start(options, ({ port }) => console.log(`Server is running on http://localhost:${port}`));
答案 1 :(得分:0)
const url = typeof process.env.url === 'string' ? process.env.url : new Error("Error Message")
if(url instanceof Error) {
throw url;
}
server.start(options, ({ port }) => console.log(`Server is running on http://localhost:${port}`));
您可以将Error Message
更改为所需的任何消息,这将使服务器断开(抛出错误)
const url = typeof process.env.url === 'string' ? process.env.url : null
if(!url) {
process.exit(0);
}
server.start(options, ({ port }) => console.log(`Server is running on http://localhost:${port}`));
答案 2 :(得分:0)
一个简单的if条件就足够了:
"display:none"