DEV Community

Cover image for Node backend port band bo'lib qolishi
Barkamol Valiev
Barkamol Valiev

Posted on

Node backend port band bo'lib qolishi

Siz Node.js serveringizni ishga tushirganingizda, "Port allaqachon ishlatilmoqda" degan xatoni ko'rganmisiz? 🚖 Bu juda bezovta qiladi, lekin buni hal qilishning oson yo'li bor!

Ushbu postda men sizga portfiner paketini ishlatib, bo'sh portni qanday topishni ko'rsataman, shunda serveringiz har doim ishlayveradi.


🛠️ Muammo: Port nomutanosibligi

Defolt holatda, ko'plab serverlar process.env.PORT yoki fallback sifatida 3000 portini ishlatadi. Ammo agar bu port allaqachon band bo'lsa, ilovangiz ishga tushmaydi. Shuning uchun, keling, biz bo'sh portni dinamik tarzda topamiz.


🗰️ Yechim:portfinder ishlatish

Install portfinder

Avvalo, portfinder paketini loyihangizga qo'shing:

npm install portfinder
Enter fullscreen mode Exit fullscreen mode

Server kodni yangilang

portfinderdan quyidagicha foydalanasiz:

const express = require("express");
const dotenv = require("dotenv");
const portfinder = require("portfinder");

const app = express();
dotenv.config();

// Define a base port to start searching from
portfinder.basePort = process.env.PORT || 3000;

portfinder.getPort((err, port) => {
  if (err) {
    console.error("Error finding available port:", err);
    return;
  }
  app.listen(port, () => {
    console.log(`Server running on port ${port}`);
  });
});
Enter fullscreen mode Exit fullscreen mode

Key Features

  1. Istalgan portdan boshlaydi: portfinder.basePort ni process.env.PORT yoki boshqa fallback portdan boshlash uchun sozlang.
  2. Ish vaqtidagi xatolardan saqlaydi: Agar istalgan port band bo'lsa, avtomatik ravishda bo'sh portni topadi.

🔥 Nima Uchun Bu Muhim

  1. Dasturlash Jarayonini Yaxshilaydi: Portni qo'lda o'zgartirishga vaqt sarflamaysiz.
  2. Ishlab Chiqarishda Ishonchlilikni Ta'minlaydi: Agar defolt port mavjud bo'lmasa ham, serveringiz ishga tushadi.

🌟 Yakuniy Fikrlar

Port muammolari sizning performance'ingizni to'xtatmasligi kerak! 🚀 portfinder yordamida, sizning Node.js serveringiz har doim ishlash uchun portni topadi.

Keyingi loyihangizda buni sinab ko'ring va qanday ishlashini izohlarda bildiring!


💡 Pro Tip: Foydalanuvchilarga qaysi port ishlatilayotganini ko'rsatish uchun do'stona console.log xabarini qo'shing.

console.log(`Server running on: http://localhost:${port}`);
Enter fullscreen mode Exit fullscreen mode

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Cloudinary image

Video API: manage, encode, and optimize for any device, channel or network condition. Deliver branded video experiences in minutes and get deep engagement insights.

Learn more

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay