Reputation: 1
I'm new to backend development and I have this following JS code using express
const express = require('express');
const app = express();
const port = process.env.PORT || '3000';
app.listen(port, ()=> console.log(`Listening on port ${port}...`));
I'm trying to change the environment variable process.env.PORT to a different port from the terminal using set PORT=5000
(I'm on windows) but whenever it runs, the value inside env.PORT is always undefined, am I doing something wrong? Also, I'm using VS Code.
Upvotes: 0
Views: 526
Reputation: 476
I am using below script to run my node app for windows.
SET NODE_ENV=development& node app.js
Upvotes: 0
Reputation: 69
In reference to your code example, you want something like this:
var express = require("express");
var app = express();
// sets port 5000 to default or unless otherwise specified in the environment
app.set('port', process.env.PORT || 5000);
app.get('/', function(req, res){
res.send('hello world');
});
// Only works on 3000 regardless of what I set environment port to or how I set
// [value] in app.set('port', [value]).
// app.listen(3000);
app.listen(app.get('port'));
process.env is a reference to your environment, so you have to set the variable there.
SET NODE_ENV=development
Upvotes: 1