@gfilho/base-server

1.0.1 • Public • Published

@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

Package Sidebar

Install

npm i @gfilho/base-server

Weekly Downloads

1

Version

1.0.1

License

MIT

Unpacked Size

26.2 kB

Total Files

17

Last publish

Collaborators

  • gfilho