-1

I have written the below code

 `use strict`;
  const express = require(`express`);

  //constansts
  const PORT = 7777;
  const HOST = `0.0.0.0`;

  //App
  const app = express();
  app.get(`/`,(req,res) => {
      res.send(`hello from nodejs`)
  });

  app.listen(PORT,HOST);
  console.log(`Running on http://${HOST}:${PORT}`);

Outcome

enter image description here

This is my package.json

 {
  "name": "samplenodejsserver",
  "version": "1.0.0",
  "description": "",
  "main": "server.js",
  "scripts": {
  "test": "echo \"Error: no test specified\" && exit 1"
 },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
  "express": "^4.18.2"
 }

}

UPDATE

It's working at HTTP ://127.0.0.1:7777 But not at HTTP ://0.0.0.0:7777

kudlatiger
  • 3,028
  • 8
  • 48
  • 98
  • 1
    Are you trying to reach the node app from the machine where you have started it? How did you started it? What does the console print? Add a error handler to catch error events. Try it with "127.0.0.1:7777" in your browser instead of "0.0.0.0:7777" There are many possibility why its not working. Add more details. – Marc Dec 01 '22 at 07:31
  • Yes, it's loading from 127.0.0.1:7777 - Yes, I am trying to access from same machine – kudlatiger Dec 01 '22 at 07:33
  • 1
    Is your browser chrome? Perhaps chrome blocks the IP "0.0.0.0" because its "not a real ip" and has a special meaning for sockets, it means "listen on all IPv4 interfaces". – Marc Dec 01 '22 at 07:38
  • Yes, it's chrome – kudlatiger Dec 01 '22 at 10:48
  • 1
    0.0.0.0 is not a valid IP. Specifying this as host to listen on only means that it will listen on all interfaces. – CherryDT Dec 02 '22 at 08:54
  • Does this answer your question? [No access to http://0.0.0.0:8000](https://stackoverflow.com/questions/68067843/no-access-to-http-0-0-0-08000) – E_net4 Dec 05 '22 at 15:50
  • 1
    See also: https://stackoverflow.com/questions/20778771/what-is-the-difference-between-0-0-0-0-127-0-0-1-and-localhost and https://stackoverflow.com/questions/1478747/why-does-ruby-on-rails-use-http-0-0-0-03000-instead-of-http-localhost3000 – E_net4 Dec 05 '22 at 15:50

1 Answers1

2

The IP 0.0.0.0 is just a wildcard which tells the server to listen at any IP, but you still have to enter a IP which points to your PC.

minomy13
  • 120
  • 1
  • 9