Переменные окружения

Файл package.json

{
  "name": "test-env-vars",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "cross-env MY_A='Maxim-Kolotovkin' MY_B='Kuklina-Nina' MY_C='218' node index.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "cross-env": "^7.0.0",
    "express": "^4.17.1"
  }
}

Файл index.js

"use strict";

const express = require("express");
const app = express();
const port = 5003;
app.listen(port);
console.log("Port: " + port);

const a = process.env.MY_A;
const b = process.env.MY_B;
const c = process.env.MY_C;

console.log("   ");
console.log(`A: ${a}`);
console.log(`B: ${b}`);
console.log(`C: ${c}`);
console.log("   ");

app.get("/", (request, response) => {
    response.end(JSON.stringify({
        a, b, c,
    }))
});

Устанавливаем библиотеки

npm install

Запускаем программу

npm start

Last updated

Was this helpful?