Home > front end >  Server is not starting or connecting when using Nodemon
Server is not starting or connecting when using Nodemon

Time:09-09

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.

mopdog@Connors-MacBook-Pro 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

CodePudding user response:

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}`);
});
  • Related