快速服务器无法连接到端口



它的工作正常,直到我做了一些改变,然后它不会连接到服务器我做了一个新的简单的服务器,但同样的问题=>它挂在这一行:

[nodemon] starting `node index.js`

,不显示server Running on port 5000

import express from 'express';
import mongoose from 'mongoose';
import cors from 'cors';
const app = express();
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(cors());
app.use('/', (req, res) => res.send('hello'));
const PORT = process.env.PORT || 5000;
const CONNECTION_URL =
'mongodb+srv://ripxxxx:ripxxxx@todo.moov4.mongodb.net/todxxxx?retryWrites=true&w=majority';
mongoose
.connect(CONNECTION_URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(
() => app.listen(PORT),
() => console.log('server Running on port ' + PORT)
)
.catch((error) => console.log(`${error} did not connect`));

包。json:

{
"name": "server",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"start": "nodemon index.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"cors": "^2.8.5",
"express": "^4.17.1",
"mongoose": "^5.13.4",
"nodemon": "^2.0.12"
}
}

删除第二个执行console.log的函数,并在app.listen中使用它,如下所示

mongoose
.connect(CONNECTION_URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(
() => app.listen(PORT, console.log('server Running on port ' + PORT))
)
.catch((error) => console.log(`${error} did not connect`));

或以下也可以,但不要这样做

.then(() => app.listen(PORT))
.then(() => console.log("Meaningless to do like this" + PORT)
.catch((error) => console.log(`${error} did not connect`));

最新更新