What port should I host my app on using the node http module?

I’m trying to host a small HTTP server on Glitch. The code currently is…

const http = require("http");

const server = http.createServer((req, res) => {
	res.writeHead(200, {
		"Content-Type": "text/plain"
	});
	res.end("Hello, world!");
});

server.on("listen", () => { console.log("\x1b[1mLISTENING\x1b[0m") }); // Logs "LISTENING" in bold
server.listen(3000); // 3000 is used as that is what another post said to do. I've also tried 80, 443, 4040, 8000, and 8080

My package.json is…

{
  "name": "my project name",
  "version": "1.0.0",
  "description": "",
  "main": "server.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node server.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "http": "^0.0.0"
  }
}

When I run node . in the terminal, it says address already in use :::3000. Is there a specific port I should use when running server.js?

Hi @Ahuman and welcome to the community! Can you please share with us your project’s public URL so we can investigate further?

@_tr

glitch runs your app based on the “start” script in your package.json. if you try to run a second instance through the terminal as you described, do expect to see that same error about the port being in use.

3000 is usually right. use the PORT environment variable if you prefer not to hard code it

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.