简体   繁体   English

使用 Nodemon 时服务器未启动或连接

[英]Server is not starting or connecting when using Nodemon

I just setup a new project using Express, Node and Nodemon.我刚刚使用 Express、Node 和 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.我设置了文件的基础知识以确保它正在启动,但是当我在终端中使用 npm start 时,它似乎已启动,但它没有收到有关服务器已启动位置的消息,并且在检查本地主机时它是未连接。 Not sure what the issue is.不确定是什么问题。

below is my package.json下面是我的 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下面是我的 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.当我在终端中运行npm start ,这是响应,然后什么也没有发生,或者从我能说的开始。

mopdog@Connors-MacBook-Pro resturant-picker % npm start             

> resturant-picker@1.0.0 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

You never run listen because it's inside another function.你永远不会运行listen ,因为它在另一个 function 中。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM