skip to Main Content

I’m trying to set up a simple Express.js server to respond with "Hello World" at the root endpoint (/). However, when I run my server and access http://localhost:3000/, I get a "cannot GET /" error in the browser. The terminal shows the message Server running on 3000, but it doesn’t return the expected response.Is there something I might be missing, or is there a specific reason why my Express route isn’t working as expected?My code Local host

I made sure the code was saved before running the server. I stopped the current server by ctrl+c and started again by using node index.js. Despite this, I still receive the "cannot GET /" error.

2

Answers


  1. Have you tried like this?

    const express = require('express');
    
    Login or Signup to reply.
  2. The problem is with the first line of your script:

    import express from "express";
    

    If you want this to work without errors, you need to add this to your package.json:

    ...
    "type": "module",
    ...
    

    Alternatively, you can also replace import express from "express"; by const express = require('express');

    In this case, you don’t need to modify your package.json

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search