Therefore, the next time you set up a web server, you will need to determine which port to listen on by checking first port and otherwise giving it a default value: javascript copy the code const app = require('http').createserver((req, res) => res.send('ahoy!')); const port = process.env.port || 3000; app.listen(port, () => { console.log(`server is listening on port ${port}`); }); the highlighted line will take the value of port if it is available, or the default value of 3000 as the secure port to listen on.
Try running the code by saving it to a file such as 11-digit phone number format philippines and server.js then running: bash copy the code node server.js the output should be a message saying server is listening on port 3000. ctrl+c and restart it using the following command: bash copy the code port=9999 node server.
Js the message should now say server is listening on port 9999 because the variable port was temporarily set for this run by the port=9999 front end node. Since process.env is just a normal object, we can set/replace values very easily: javascript copy the code process.env.my_variable = 'ahoy'; the above code will set or override the value my_variable.
Stop the server using the keys
-
- Posts: 18
- Joined: Sun Dec 22, 2024 4:29 am