silent_coder
silent_coder

Reputation: 6522

how to run node.js app from cmd with some predefined port in Windows

Is it some command exist to run my node.js code with directly specified port? something like node server.js port=7777 ?

my server.js code looks like this

http.createServer(function (req, res) {
  if (mount(req, res)) return;
}).listen(process.env.PORT || 80);

Upvotes: 10

Views: 30717

Answers (5)

Bernard
Bernard

Reputation: 1238

Use this on the command line:

http-server -p 7777

Upvotes: 0

user585776
user585776

Reputation:

On ubuntu I just do

PORT=7777 node .

in commandline, no set or export needed.

Upvotes: 7

Yash Bhayre
Yash Bhayre

Reputation: 11

for linux export PORT=5000; node index.js

for windows set PORT=5000; node index.js

in case of powerShell

set $env:PORT=5000; node index.js

const port = process.env.PORT || 3000; app.listen(port,()=>{console.log(Listening on port ${port})})

Upvotes: 1

Ph0en1x
Ph0en1x

Reputation: 10067

A simple adding to Alberto's answer. On Windows machine you haven't export command in cmd, use set instead. Then the whole script will be looks like this:

set PORT=7777
node server.js

Note that the syntax in PowerShell is slightly different:

$env:PORT=7777
node server.js

Upvotes: 14

Alberto Zaccagni
Alberto Zaccagni

Reputation: 31580

Depending on what server.js contains you should be able to do so.

At a minimum you should read port (you could use https://github.com/substack/node-optimist)

var argv = require('optimist').argv;
console.log(argv.port);

// use it like this
$ node server.js -port 7777

and then listen to it on your server (this depends on what lib you're using).

Run the server like this

export PORT=7777; node server.js

Upvotes: 12

Related Questions