1.0.1 • Published 5 years ago

@gfilho/base-server v1.0.1

Weekly downloads
3
License
MIT
Repository
github
Last release
5 years ago

@gfilho/base-server

A lightweight wrapper for Express that allows easily setup HTTP headers and SSL.

Codacy Badge Build Status codecov

Getting Started

Installation

npm install @gfilho/base-server --save

Simple Server

This is example show how create a simple REST server using the @gfilho/base-server:

var Server = require('@gfilho/base-server');

// Setup Server
const config = {
  router: {
    address: 'api',
    port: 1234,
  },
};

// Instance of Server
const server = new Server(config);

// Bind requests
server.get('/ping', (req, res) => {
  res.json({ msg: 'Ops! I received a GET' });
});

// Run Server
server.start();

Complete Server

This is example show a REST server using all features allowed by @gfilho/base-server:

var Server = require('@gfilho/base-server');

// Setup Server
const config = {
  router: {
    address: 'api',
    port: 1234,
    header: {
      allowedMethods: 'GET PUT POST DEL',
      allowedHost: 'mydomain.com',
      allowedHeaders: 'MySpecialHeader',
      allowedCredentials: true,
    },
    ssl: {
      key: 'examples/ssl/key.pem',
      certificate: 'examples/ssl/server.crt',
    },
  },
};

// Instance of Server
const server = new Server(config);

// Bind requests
server.get('/ping', (req, res) => {
  res.json({ msg: 'Server https is alive' });
});

server.put('/ping', (req, res) => {
  res.json({ msg: 'Ops! I received a PUT' });
});

server.post('/ping', (req, res) => {
  res.json({ msg: 'Ops! I received a POST' });
});

server.delete('/ping', (req, res) => {
  res.json({ msg: 'Ops! I received a DEL' });
});

// Run Server
server.start();

License

MIT