NodeJS 将节点js与mongoDB连接时出错

e5nqia27  于 2023-02-08  发布在  Node.js
关注(0)|答案(1)|浏览(274)

In the Backend folder I created server.js file.

const express = require('express');
const cors = require('cors');
const mongoose = require('mongoose');

require('dotenv').config();

const app = express();
const port = process.env.PORT || 5000;

app.use(cors());
app.use(express.json());

//connect with mongoDB
const uri = process.env.ATLAS_URI;
mongoose.connect(uri, { useNewUrlParser: true, useCreateIndex: true }
);
const connection = mongoose.connection;
connection.once('open', () => {
  console.log("MongoDB database connection established successfully");
})

//import routes
//const exercisesRouter = require('./routes/exercises');
//const usersRouter = require('./routes/users');

//use routes
//app.use('/exercises', exercisesRouter);
//app.use('/users', usersRouter);

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

after I entered nodemon server command.
Here What printed in the terminel
[nodemon] 2.0.20 [nodemon] to restart at any time, enter rs [nodemon] watching path(s): . [nodemon] watching extensions: js,mjs,json [nodemon] starting node server.js (node:92140) [MONGOOSE] DeprecationWarning: Mongoose: the strictQuery option will be switched back to false by default in Mongoose 7. Use mongoose.set('strictQuery', false); if you want to prepare for this change. Or use mongoose.set('strictQuery', true); to suppress this warning. (Use node --trace-deprecation ... to show where the warning was created) node:events:491 throw er; // Unhandled 'error' event ^
Error: listen EADDRINUSE: address already in use :::5000 at Server.setupListenHandle [as _listen2] (node:net:1733:16) at listenInCluster (node:net:1781:12) at Server.listen (node:net:1869:7) at Function.listen (/Users/masterlwa/Desktop/exercise-tracker/backend/node_modules/express/lib/application.js:635:24) at Object. (/Users/masterlwa/Desktop/exercise-tracker/backend/server.js:32:5) at Module._compile (node:internal/modules/cjs/loader:1218:14) at Module._extensions..js (node:internal/modules/cjs/loader:1272:10) at Module.load (node:internal/modules/cjs/loader:1081:32) at Module._load (node:internal/modules/cjs/loader:922:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) Emitted 'error' event on Server instance at: at emitErrorNT (node:net:1760:8) at process.processTicksAndRejections (node:internal/process/task_queues:82:21) { code: 'EADDRINUSE', errno: -48, syscall: 'listen', address: '::', port: 5000 }
Node.js v18.13.0 [nodemon] app crashed - waiting for file changes before starting...
Currently I do not have I regarding how to fix this. How to fix this?

vlurs2pr

vlurs2pr1#

似乎端口5000已被另一个应用程序使用,请尝试将端口更改为5001或其他端口

相关问题