1

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. **

1 Answer 1

1

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}`);
});
Sign up to request clarification or add additional context in comments.

4 Comments

Note: I changed port as I use port 3000 frequently in other node apps
You shouldn't supplement your answer in comments. Please improve it by editing.
You are close?? What that means?? Yes I import the npm package Express both imported but still not get any response
@AkhilRana, I only added the first line to what you had, and thus, I say you were close. You installed the package, but that is not the same as importing the expess module for node to use. nodejs.org/docs/latest-v12.x/api/modules.html#modules_modules

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.