जब मैं nodemonindex.js चलाने की कोशिश करता हूं तो मुझे यह त्रुटि मिलती है मेरे पास pm2 चल रहा है लेकिन मेरा लोकलहोस्ट / 5002 देता है
[nodemon] starting `node index.js`
Listening on 5002
events.js:377
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE: address already in use :::5002
at Server.setupListenHandle [as _listen2] (net.js:1331:16)
at listenInCluster (net.js:1379:12)
at Server.listen (net.js:1465:7)
at Function.listen (/home/vivek/mycodes/ask-backend/node_modules/express/lib/application.js:618:24)
at Object.<anonymous> (/home/vivek/mycodes/ask-backend/index.js:19:5)
at Module._compile (internal/modules/cjs/loader.js:1085:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
at Module.load (internal/modules/cjs/loader.js:950:32)
at Function.Module._load (internal/modules/cjs/loader.js:790:12)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
Emitted 'error' event on Server instance at:
at emitErrorNT (net.js:1358:8)
at processTicksAndRejections (internal/process/task_queues.js:82:21) {
code: 'EADDRINUSE',
errno: -98,
syscall: 'listen',
address: '::',
port: 5002
}
[nodemon] app crashed - waiting for file changes before starting...
मैंने कोशिश की pm2 kill
killall node
अभी भी पोर्ट मरता नहीं है, जब मैंने जीथब से अपना कोड खींचा और एक नई मशीन पर चल रहा था, तो मुझे नहीं पता कि डीबी इस त्रुटि के लिए रेजोन है या नहीं
Index.js
var moment = require("moment");
var express = require("express");
var ObjectId = require("mongodb").ObjectID;
const { ExploreTrendRequest } = require("g-trends");
var bodyParser = require("body-parser");
var app = express();
// if (process.env.NODE_ENV == "production") {
// app.use(express.static("portfolio/build"));
// const path = require("path");
// app.get("*", (req, res) => {
// res.sendFile(path.resolve(__dirname, "portfolio", "build", "index.html"));
// });
// }
const PORT = process.env.PORT || 5002;
express().listen(PORT, () => console.log(`Listening on ${PORT}`));
app.listen(process.env.PORT || 5002);
const multer = require("multer");
const upload = multer();
var bodyParser = require("body-parser");
app.use(express.json());
var MongoClient = require("mongodb").MongoClient;
var id = require("mongodb").ObjectID;
//Create a database named "mydb": sudo service mongod start
var url = "mongodb://localhost:27017/";
MongoClient.connect(url, { useUnifiedTopology: true }, function (err, db) {
if (err) throw err;
var mydb = db.db(mydb);
var created = moment().format("YYYY-MM-DD hh:mm:ss");
// mydb.createCollection("questions", function (err, data) {
// if (err) throw err;
// var question = {
// question: "the first question",
// time: created,
// };
// mydb.collection("questions").insertOne(question, function (err, data) {
// if (err) throw err;
// console.log("Data created!");
app.use(function (req, res, next) {
// Website you wish to allow to connect
res.setHeader("Access-Control-Allow-Origin", "http://localhost:3000");
// Request methods you wish to allow
res.setHeader(
"Access-Control-Allow-Methods",
"GET, POST, OPTIONS, PUT, PATCH, DELETE"
);
// Request headers you wish to allow
res.setHeader(
"Access-Control-Allow-Headers",
"X-Requested-With,content-type"
);
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader("Access-Control-Allow-Credentials", true);
// Pass to next layer of middleware
next();
});
app.use(express.urlencoded({ extended: true }));
app.post("/questionpost", function (req, res) {
let formData = req.body;
// let bodyJson = JSON.parse(formData);
console.log(formData);
mydb.collection("questions").insertOne(formData, (err, result) => {
if (err) {
res.send({ error: "Ann error has occured" });
} else {
res.send(result.ops[0]);
}
});
});
app.post("/answerpost", function (req, res) {
let formData = req.body;
// let bodyJson = JSON.parse(formData);
// console.log(formData);
mydb.collection("answers").insertOne(formData, (err, result) => {
if (err) {
res.send({ error: "Ann error has occured" });
} else {
res.send(result.ops[0]);
}
});
});
app.get("/answersapi/:gotid", function (req, res) {
let id = req.params.gotid;
console.log(id);
mydb
.collection("answers")
.find({ question_id: id })
.toArray(function (err, data) {
if (err) throw error;
res.send(data);
});
});
// mydb
// .collection("questions")
// .insertOne(formData)
// .toArray(function (err, res) {
// if (err) throw err;
// console.log("1 document inserted");
// });
// res.send(res);
// app.post("/questionpost", jsonParser, function (req, res) {
// let data = req;
// console.log(data);
// });
app.get("/questapi", function (req, res) {
console.log("apicalled");
mydb
.collection("questions")
.find({})
.toArray(function (err, data) {
if (err) throw error;
res.send(data);
});
});
//http://localhost:5002/answerpost
// increment;
app.post("/increment", function (req, res) {
let id = req.body;
var correctcount = id.correctcount + 1;
//var ansid = id.Answer_id;
var realid = ObjectId(id.Answer_id);
console.log(correctcount);
// var myquery = { address: "Valley 345" };
// var newvalues = { $set: { name: "Mickey", address: "Canyon 123" } };
// dbo
// .collection("customers")
// .updateOne(myquery, newvalues, function (err, res) {
// if (err) throw err;
// console.log("1 document updated");
// db.close();
// });
mydb
.collection("answers")
.updateOne(
{ _id: realid },
{ $set: { correctcount: correctcount } },
function (err, data) {
if (err) throw err;
console.log("1 document updated");
res.send(data);
}
);
});
app.post("/decrementer", function (req, res) {
let id = req.body;
var wrongcount = id.wrongcount - 1;
//var ansid = id.Answer_id;
var realid = ObjectId(id.Answer_id);
console.log(wrongcount);
// var myquery = { address: "Valley 345" };
// var newvalues = { $set: { name: "Mickey", address: "Canyon 123" } };
// dbo
// .collection("customers")
// .updateOne(myquery, newvalues, function (err, res) {
// if (err) throw err;
// console.log("1 document updated");
// db.close();
// });
mydb
.collection("answers")
.updateOne(
{ _id: realid },
{ $set: { wrongcount: wrongcount } },
function (err, data) {
if (err) throw err;
console.log("1 document updated");
res.send(data);
}
);
});
app.get("/questone/:gotid", function (req, res) {
let id = req.params.gotid;
// /console.log(id);
mydb
.collection("questions")
.find({ _id: ObjectId(id) })
.toArray(function (err, data) {
if (err) throw error;
//console.log(data);
res.send(data);
});
});
//trend api call
app.post("/trend", function () {
const explorer = new ExploreTrendRequest();
console.log("api calling 1");
explorer
.addKeyword("Dream about snakes")
.compare("Dream about falling")
.download()
.then((csv) => {
console.log(
"[✔] Done, take a look at your beautiful CSV formatted data!"
);
console.log(csv);
})
.catch((error) => {
console.log("[!] Failed fetching csv data due to an error", error);
});
});
});
मैंने अपना index.js यहां पोस्ट किया है ताकि आप यह बता सकें कि त्रुटि क्या है
1 उत्तर
यदि यह किसी अन्य कंप्यूटर पर काम करता है, तो शायद यह आपके ऐप में कोई समस्या नहीं है। पोर्ट पहले से ही किसी अन्य प्रक्रिया द्वारा उपयोग में है। शायद एक और आवेदन। आप क्या करना चाहते हैं
- उस प्रक्रिया की पहचान करें जो पहले से ही पोर्ट का उपयोग कर रही है
- उसे मार
यदि आप Mac का उपयोग करते हैं, तो आप पहले sudo lsof -i -P | grep LISTEN
चलाकर ऐसा करते हैं। यह उन सभी प्रक्रियाओं को सूचीबद्ध करेगा जो किसी भी पोर्ट पर सुन रहे हैं। पोर्ट 5002 पर चलने वाले को ढूंढें (चूंकि यह वही है जिसका आप यहां उपयोग कर रहे हैं)। तालिका में दूसरा कॉलम जो lsof उत्पादित करता है वह एक प्रक्रिया संख्या है। अब आप kill -9 <process number>
चला सकते हैं। उसके बाद आपका आवेदन काम करना चाहिए
संबंधित सवाल
नए सवाल
node.js
Node.js एक घटना-आधारित, गैर-अवरोधक, अतुल्यकालिक I / O रनटाइम है जो Google के V8 जावास्क्रिप्ट इंजन और libuv लाइब्रेरी का उपयोग करता है। इसका उपयोग उन अनुप्रयोगों को विकसित करने के लिए किया जाता है जो क्लाइंट पर और साथ ही सर्वर साइड पर जावास्क्रिप्ट को चलाने की क्षमता का भारी उपयोग करते हैं और इसलिए कोड के पुन: प्रयोज्य और संदर्भ स्विचिंग की कमी से लाभान्वित होते हैं।
sudo lsof -i -P | grep LISTEN
क्या प्रिंट करता है? आप कौन सा ओएस चला रहे हैं? खिड़कियाँ? लिनक्स? मैक ओएस? आप अपना आवेदन कैसे शुरू करते हैं?