Environment variables are generally provided by the hosting environments like Heroku.
For our testing locally, set environment variables in the terminal and run the node script from the same terminal.
To use a dynamic port instead of setting hardcoded, use env variable PORT.
const express = require("express");
const app = express();
app.get("/", (req, res) => {
res.send("Hello");
});
app.get("/api/courses", (req, res) => {
res.send([1, 2, 3]); //courses array
});
//app.listen(3000, () => console.log("listening"));
const port = process.env.PORT || 3000
app.listen(port,()=>console.log(`Listening on port ${port}`));
For testing on terminal.
export PORT=5000
chome: http://localhost:5000
No comments:
Post a Comment