Creating a global variable in Node.js

Adding a global variable in Node.js

The global object in Node.js allow us to define a variable in the global scope, unlike browsers where it can be done using the var keyword. var something inside a Node.js module will be local to that module.

const http = require("http");
const app = require('./app/app.js');

global.yamicode = "hello from server.js";

const port = process.env.PORT || "3100";

const server = http.createServer(function (req, res) {
  app.global(req, res);
exports.global = function(req, res){

  res.writeHead(200, {"Content-Type": "text/html"});

  //Use the global variable
  res.write(yamicode);

  res.end();

};