Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

server-modules

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

server-modules

Easy configurable server modules (logging, express, api, websocket, mongodb)

  • 0.1.8-dev.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

server-modules

CircleCI

Features

This packages provides some easy-to-use utilities to setup a server with node.js.

  • Logging (stdout, file and mail)
  • MongoDB connector
  • Express wrapper
  • Websocket wrapper
  • API creator

How to use

Create a new ServerEnvironment

import { ServerEnvironment } from 'server-modules';

// check /modules/logger/_interface.d.ts for additional options
const Server = new ServerEnvironment(
    {
        ident: 'Example-Server',
        logger: {
            console: {
                level: 'silly'
            },
            file: {
                path: './logs',
                levels: ['info', 'warn', 'error']
            }
        }
    }
);

Logger

const logger = Server.logger.spawn('my-module');
logger.info(`Logger created`);

Express

Server
    .createExpress({
        port: 8080,
        init: app => {
            /* 
            * setup app
            * this will be applied after
            * cookieParser() / bodyParser.json() / compression() / morgan()
            * the server will send a 404 status message if no route was found
            */
        }
    });

API

import { ApiSegment } from 'server-modules';

export const MyAPISegment = new ApiSegment('api');

MyAPISegment
    .addRoute<{txt:string}>('echo/:txt')
    .get((req, res) => {
        const { params } = req;
        res.send(params.txt);
    });

Server
    .createExpress({
        port: 8081,
        init: [MyAPISegment]
    });

Server
    .createExpress({
        port: 8082,
        init: app => {
            MyAPISegment.registerOn(Server.logger, app);
        }
    });

Websocket

Server
    .createWebsocket(
        {
            port: 3001,
            connection: MyWebsocketConnection
        });
import { WebsocketConnection } from 'server-modules';

export class MyWebsocketConnection extends WebsocketConnection {
    init() {
        /* register event listeners */
        this._socket.on('ping', () => {
            this._socket.emit('pong');
        });
    }
}

MongoDB

const Client = Server.connectMongoDb({ auth });
Client.connect(db => {
    /* work on database or save reference to db */
});
interface MongoDbConnectionCfg {
    port?: number;          // default: 27017
    host?: string;          // default: localhost
    auth?: {                // default: undefined
        database?: string;  // default: admin
        user: string;
        password: string;
    };
    options?: MongoClientOptions;   // default: {}
}

Keywords

FAQs

Package last updated on 23 Mar 2018

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc