Logo

dev-resources.site

for different kinds of informations.

Node backend port band bo'lib qolishi

Published at
1/1/2025
Categories
node
webdev
express
tutorial
Author
jsdecoder
Categories
4 categories in total
node
open
webdev
open
express
open
tutorial
open
Author
9 person written this
jsdecoder
open
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
express Article's
30 articles in total
Favicon
Cookies auto clearing after browser refresh issue , CORS related express cookies issue
Favicon
how to setup express api from scratch
Favicon
I Really like Middleware in NodeJs/Express.
Favicon
From Legacy to Lightning: Modernizing an Astro App with Daytona
Favicon
Setting Up Dual Compilation (SSR + CSR) in ViteJS with vite-plugin-builder
Favicon
Starting A Series On TypeScript Join In As We Build Together
Favicon
How to Generate a Secure JWT Secret Using Node.js
Favicon
Mastering Express.js: A Deep Dive
Favicon
Comprendre le Design Pattern MVC avec Node.js, Express et MongoDB
Favicon
How to implement File uploads in Nodejs: A step by step guide
Favicon
Node backend port band bo'lib qolishi
Favicon
Deploying an Existing Express API + Prisma + Supabase Project to Vercel
Favicon
New React Library: API Integration Made Easy with Axiosflow's Automatic Client Generation
Favicon
Build and Deploy a Monorepo WebSocket web application with Turbo, Express, and Vite on Render Using Docker
Favicon
Express app with decorators based routing and dependency injection
Favicon
miniframe-router: Router for Express.JS Applications
Favicon
Create an API for AG-Grid with Express
Favicon
Blogsphere | A blogging website made with MERN stack. includes user management.
Favicon
วิธีทำ Auth API ด้วย Express, JWT, MySQL และ Prisma
Favicon
[Boost]
Favicon
Complete, full-stack setup for any node/express/psql app, equipped with basic ui, routes & more.
Favicon
Which One Should You Choose NEST JS or EXPRESS JS?
Favicon
Hackers Love These Common MERN Stack Mistakes: Are You Exposing Your App? 🔐
Favicon
Build a React login page template
Favicon
A New Way of Setting Up an Express Server for Lazy Developers npm i mbfi
Favicon
[Boost]
Favicon
Express request types
Favicon
Mastering Express.js: A Deep Dive
Favicon
Getting Started with Express.js for Web Development
Favicon
Introduction to TypeScript with Express: Building Your First REST API

Featured ones: