node.jsexpressserverlocalhostweb-development-server

Express.js server .get() not responding on localhost


I'm currently learning web development and working with Express.js and Node.js. The issue I'm encountering is when I try to access http://localhost:3000/ or http://localhost:3001/ (I've also tried changing the port to 3001) in my web browser , nothing happens. I'm not receiving any response from the server and "Test1" is not even printing/outputing/logging on the terminal.

I've set up a simple Express server with the following code:

import express from "express";
const app = express();
const port = 3000;

app.get('/', (req, res) => {
  console.log("Test1");
  res.send('Hello World!');
});

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`);
});

I've checked the server console and there are no error messages.

This is what the explorer looks like: explorer

This is what the terminal shows when I run the command node index.js: terminal

This is what the package.json file contains: package.json

I have node version v22.1.0. installed on my wsl/vsCode.

Could someone please help me troubleshoot this issue? I've used similar code before and i could have sworn it worked fine, but now it's not responding and I'm not sure what could be causing the problem. I've spent hours trying to troubleshoot; this a desperate attempt to resolve the issue lol. Would appreciate the help!


Solution

  • This was the fix for me (suggestion by @Ravi Yadav):

    What fixed it for me was using a separate port, in my case: 8080. and entered http://localhost:8080/ instead of http://localhost:3000/ I just played around with different ports to see which ones worked.