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
Have you tried like this?
The problem is with the first line of your script:
If you want this to work without errors, you need to add this to your package.json:
Alternatively, you can also replace
import express from "express";
byconst express = require('express');
In this case, you don’t need to modify your package.json