Server is not starting or connecting when using Nodemon

Issue

I just setup a new project using Express, Node and Nodemon. I setup the basics of the file to make sure it is starting but when I’m using npm start in the terminal it appears to start but it doesn’t get a message of where the server has been started and when checking the localhost it is not connected. Not sure what the issue is.

below is my package.json

{
  "name": "resturant-picker",
  "version": "1.0.0",
  "description": "Resturant picker app for Alchs",
  "main": "server.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "nodemon server.js"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/connordensmore/resturant-picker.git"
  },
  "keywords": [
    "crud",
    "mongodb"
  ],
  "author": "Connor",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/connordensmore/resturant-picker/issues"
  },
  "homepage": "https://github.com/connordensmore/resturant-picker#readme",
  "dependencies": {
    "axios": "^0.27.2",
    "body-parser": "^1.20.0",
    "dotenv": "^16.0.2",
    "ejs": "^3.1.8",
    "express": "^4.18.1",
    "mongoose": "^6.5.5",
    "morgan": "^1.10.0",
    "node": "^18.8.0",
    "nodemon": "^2.0.19"
  }
}

below is my server.js

const express = require("express");
const app = express();

const PORT = process.env.PORT || 3000;

app.get("/", (req, res) => {
  res.send("Crud App");

  app.listen(PORT, () => {
    console.log(`Server is running on http://localhost:${PORT}`);
  });
});

when i run npm start in the terminal this is the response and then nothing happens or is started from what i can tell.

[email protected] resturant-picker % npm start             

> [email protected] start
> nodemon server.js

[nodemon] 2.0.19
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node server.js`
[nodemon] clean exit - waiting for changes before restart

Solution

You never run listen because it’s inside another function.

app.get("/", (req, res) => {
  res.send("Crud App");
});

app.listen(PORT, () => {
  console.log(`Server is running on http://localhost:${PORT}`);
});

Answered By – Konrad Linkowski

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published