#run nestjs api in different ports, read port from command line

1 messages · Page 1 of 1 (latest)

pliant bough
#

Hello everyone,

like in react or next, we can specify the port of our application by provifing it in the command line, like thins :
yarn start -p 3001 (the default port is 3000)

I ask how to can do it with nestjs. how can I read the port from the user and put it at app.listen(READED_PORT). or other mechanism for starting the application in different ports witout changing the .env file or PORT config ?

I hope that my question is clear !

Kind regards 😀

edgy eagle
#

Do you currently use the PORT from config?

pliant bough
#

no, I use it from .env file

edgy eagle
#

Okay, so you get the port from config already, right?

pliant bough
#

and I use dotenv package, to get PORT via process.env.PORT

#

and passed it into app.listen()

edgy eagle
#

If so, you can so PORT=3001 yarn start to override the value that is in the .env (Windows has a different syntax but the idea is the same)

pliant bough
#

What's about If I want to use the react style using flag like --port 3000 or -p 3000 ?

edgy eagle
#

You'd have to create a way to read that flag. Nest's CLI doesn't provide that

#

Technically you'd actually have to use nest start -e 'node -p 3001' to set the execution because nest doesn't forward on flags it doesn't know about

pliant bough
#

My goal by running the api in different ports is to run the application in different processes , then try to do a load balancing for the free processes to make the application scalable.

#

is there a way to do that nativeley by nestjs ?

edgy eagle
#

I would just use nginx for that, I think

#

Nest's CLI is really for dev mode only and other than that it's pretty much just a structured way to write a node server

pliant bough
#

I found a pakage called PM2, it is a Node.js Production Process Manager with a built-in Load Balancer.

#

is there a wrapper for nestjs for this pakage ?

#

I try to use it with my typescript project to do something like this .

edgy eagle
#

Why need a nest wrapper for that? Pm2 is a process manager that can run multiple processes (servers) for you. What framework you're using is irrelevant

pliant bough
#

or what is the builtin features for more scalibity that we have right now for nestjs ?

edgy eagle
#

Again, nest is just a structured framework for writing a node server.

pliant bough
#

Ok good, thanks , I get it 😀

ember echo