🚨 Shai-Hulud Strikes Again:834 Packages Compromised.Technical Analysis →
Socket
Book a DemoInstallSign in
Socket

@everymundo/runner

Package Overview
Dependencies
Maintainers
26
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@everymundo/runner

Runs a given script only if it is the main one called with node

latest
Source
npmnpm
Version
1.1.2
Version published
Maintainers
26
Created
Source

@everymundo/runner

Runs a given script only if it is the main one called with node.

This module/package aims to help organizing main node scripts and to help with testing them as well.

Installation

npm install @everymundo/runner

Example

Let's say you have a node.js file with a small simple http server

// server.js
const http = require('http');
const logr = require('@everymundo/simple-logr');
const APP_PORT = Math.abs(process.env.APP_PORT) || 8008;

const requestHandler = (req, res) => {
  logr.debug(req.url);
  res.end('Hello Node.js Server!');
};

const server = http.createServer(requestHandler);

server.listen(APP_PORT, (err) => {
  if (err) {
    logr.error('something bad happened', err);
    throw err;
  }

  logr.info(`server runnin on port ${APP_PORT}`);
});

It is a regular working code, and you can put it to work by just running:

node server.js

But it is not really easy to test it. As soon as this file is loaded by your test 'test/server.test';

require('../server.test');

That will automatically put the server to run, and that is not an ideal effect when you only want to test it.

Here comes the runner

By slightly changing the code ...

// server.js
const { run } = require('@everymundo/runner');
const http = require('http');
const logr = require('@everymundo/simple-logr');
const APP_PORT = Math.abs(process.env.APP_PORT) || 8008;

const requestHandler = (req, res) => {
  logr.debug(req.url);
  res.end('Hello Node.js Server!');
};

const server = http.createServer(requestHandler);
const initialize = () => server.listen(APP_PORT, (err) => {
  if (err) {
    logr.error('something bad happened', err);
    throw err;
  }

  logr.info(`server runnin on port ${APP_PORT}`);
});

run(__filename, initialize);

... we can guarantee that it will only run its functionality when used by running node server.js but not when loading the file via require('server.js');

Keywords

node

FAQs

Package last updated on 28 May 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts