1.1.3 • Published 3 years ago

@maltem-canada/express-server v1.1.3

Weekly downloads
93
License
MIT
Repository
gitlab
Last release
3 years ago

Maltem Canada - Express Server

install

npm i @maltem-canada/express-server

Server

When instanciating your server, you must at least provide a port and a name.

import Server from '@maltem-canada/express-server';
import routes from './routes';
import config from './config';

const server = new Server({
  port: config.port,
  name: 'microservice',
  routes,               // optional
  mongo: config.mongo   // optional
});

export default function start() {
  server.start();
}

getConfig

Function used to validate the env variable format (upper case)

usage

import { getConfig } from '@maltem-canada/express-server';

export default {
  port: getConfig('PORT'),
  mongo: {
    url: getConfig('MONGO_URL'),
    name: getConfig('APP_NAME'),
  },
};

mustProvide

Function used to throw an error if the specified field is not provided

usage

import { mustProvide } from '@maltem-canada/express-server';

function myFunction(param = mustProvide('param')) {
  // Param provided
}

mustBeType

Function used to check the type of a variable

supported types: ['string', 'number', 'float', 'object', 'array']

usage

import { mustBeType } from '@maltem-canada/express-server';

console.log(mustBeType('object', {})) // true
console.log(mustBeType('array', [])) // true
console.log(mustBeType('number', 12)) // true

logger

Function used to log info or errors

usage

import { logger } from '@maltem-canada/express-server';

const message = 'Hi boy!';
logger.info(message);
const error = new Error();
error.message = 'You failed';
error.status = 500;
logger.error(error);

mongoCollection

Function to return a mongo collection

usage

import { mongoCollection } from '@maltem-canada/express-server';

const userCollection = mongoCollection('user');
userCollection.insertOne({
  name: 'toto',
  age: 29
});