Akhil Rana
Akhil Rana

Reputation: 155

Localhost didn’t send any data. ERR_EMPTY_RESPONSE , Nodejs error

I just started learning Node.js, but I'm stuck on a server issue. I tried all sorts of solutions but they didn't work (changed port, updated node.js, updated chrome, etc) but nothing has worked.

THE SERVER OPENS BUT LOCALHOST DOES NOT RESPOND.

Here is my index.js file

const app = express();
const port = 3000;

app.get("/", (req, res) => {
  console.log("test");
  res.send("Hello world");
});

app.listen(port, () => {
  console.log(`Server open port ${port}`);
});

Here is my package.json file

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

Terminal views

$ node app.js
Server open port 3000
test
test
test
test

**The server starts but didn't get a response by localhost in Chrome. The same code works in my friends device though. **

Upvotes: 1

Views: 2707

Answers (1)

Alex
Alex

Reputation: 11

You are close. Did you import the module?

const express = require('express');
const port = 3003;
const app = express();

app.get("/", (req, res) => {
    console.log("test");
    res.send("Hello world");
});

app.listen(port, () => {
    console.log(`Server open port ${port}`);
});

Upvotes: 1

Related Questions