NodeJS 无法在Firebase函数模拟器上提供Express应用程序|EADDRINUSE:地址已被用途:::3000

snz8szmq  于 2023-03-29  发布在  Node.js
关注(0)|答案(2)|浏览(119)

我正在使用firebase函数在firebase上测试express应用程序部署。但在使用firebase serve命令后。我得到了EADDRINUSE: address already in use :::3000。这是我的index.js

const functions = require('firebase-functions');
const express = require('express');
const validator = require('email-validator');
const PORT = 3000;
const app = express();

/* JSON body parse*/
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

app.get('/hello', (req, res, next) => {
  console.info('/hello call success ');
  res.send('Welcome to Firebase Cloud Functions');
});

app.post('/emailValidate', async (req, res, next) => {
  const postData = req.body;
  if (postData.email) {
    console.info('/emailValidate call success ');
    res.json({ 'status': validator.validate(postData.email) });
  } else {
    console.warn('/emailValidate wrong input ');
    res.status(500).json({ 'status': 'wrong input' });
  }
});

app.listen(PORT, () => {
  console.info('Server is running on PORT:', PORT);
});

exports.app = functions.https.onRequest(app);

package.json

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "scripts": {
    "serve": "firebase emulators:start --only functions",
    "shell": "firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "engines": {
    "node": "16"
  },
  "main": "index.js",
  "dependencies": {
    "body-parser": "^1.19.1",
    "email-validator": "^2.0.4",
    "express": "^4.17.2",
    "firebase-admin": "^9.8.0",
    "firebase-functions": "^3.14.1"
  },
  "devDependencies": {
    "firebase-functions-test": "^0.2.0"
  },
  "private": true
}

打开URL functions[us-central1-app]: http function initialized (http://localhost:5001/{app-name}/us-central1/app).时出错

node:events:368
>        throw er; // Unhandled 'error' event
>        ^
>
>  Error: listen EADDRINUSE: address already in use :::3000
>      at Server.setupListenHandle [as _listen2] (node:net:1334:16)
>      at listenInCluster (node:net:1382:12)
>      at Server.listen (node:net:1469:7)
>      at Function.listen (\home\development\express-firebase\functions\node_modules\express\lib\application.js:618:24)
>      at Object.<anonymous> (D:\002_Research_Development\Web\development\express-firebase\functions\index.js:29:5)
>      at Module._compile (node:internal/modules/cjs/loader:1101:14)
>      at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
>      at Module.load (node:internal/modules/cjs/loader:981:32)
>      at Function.Module._load (node:internal/modules/cjs/loader:822:12)
>      at Module.require (node:internal/modules/cjs/loader:1005:19)
>  Emitted 'error' event on Server instance at:
>      at emitErrorNT (node:net:1361:8)
>      at processTicksAndRejections (node:internal/process/task_queues:83:21) {
>    code: 'EADDRINUSE',
>    errno: -4091,
>    syscall: 'listen',
>    address: '::',
>    port: 3000
>  }

此外,我已经检查过在定义的端口上没有运行任何东西。甚至尝试在index.js中更改端口号,但问题仍然存在。
任何帮助都将不胜感激:)

knpiaxh1

knpiaxh11#

另一个应用程序正在使用端口3000,您需要停止应用程序和/或杀死端口,这取决于您的操作系统。

vm0i2vca

vm0i2vca2#

只是为了详细说明前面的答案,我在app.listen函数中使用了一个名为“PORT”的环境变量,这导致了错误,当我在listen函数中硬编码端口号时,它被解决了

相关问题