i229194964
@i229194964
Веб разработчик

Как исправить Server running at undefined:undefined/?

Хочу запустить бота на локальном сервере при запуске бота выдает Server running at http://undefined:undefined/ в консоли как исправить ?
hello.js
const fs = require('fs');
const http = require('http');
const path = require('path');

const { APP_PORT, APP_IP } = process.env;

const indexPath = path.join(__dirname, 'index.html');

const server = http.createServer((req, res) => {
  fs.readFile(indexPath, (err, data) => {
    if (err) {
      res.writeHead(400, {'Content-Type': 'text/plain'});
      res.write('index.html not found');
    } else {
      res.writeHead(200, {'Content-Type': 'text/html'});
      res.write(data);
    }
    res.end();
  });
}).listen(APP_PORT, APP_IP, () => {
  console.log(`Server running at http://${APP_IP}:${APP_PORT}/`);
});

process.on('SIGTERM', () => {
  console.info('SIGTERM signal received.');
  console.log('Closing http server.');
  server.close(() => {
    console.log('Http server closed.');
    process.exit(0);
  });
});

package.json
{
    "name": "my-node-app",
    "version": "1.0.0",
    "description": "My Node.js App",
    "main": "hello.js",
    "scripts": {
        "start": "node hello.js"
    },
    "author": "Ваше имя",
    "license": "MIT",
    "dependencies": {
        "dotenv": "^16.3.1",
        "fs": "^0.0.1-security",
        "http": "^0.0.1-security",
        "path": "^0.12.7"
    }
}

.env
APP_PORT=3000
APP_IP=127.0.0.1
  • Вопрос задан
  • 77 просмотров
Решения вопроса 1
@sergiodev
Пропишите переменные среды APP_IP и APP_PORT в командной оболочке перед запуском сервера вместо .env (через export, если используется sh/bash и т.п., на винде по-другому).

Node.js сама по себе не читает эти файлы, вам нужен сторонний пакет для этого (как уже упомянул szQocks в комментах).
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы