🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
DemoInstallSign in
Socket

x-easy

Package Overview
Dependencies
Maintainers
1
Versions
57
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

x-easy

Make simple and modular, building a web application with express.

2.12.0
npm
Version published
Weekly downloads
76
590.91%
Maintainers
1
Weekly downloads
 
Created
Source

X-Easy

This is a simple way to raise a server in Node.JS.

Module installation

npm install x-easy --save

// or

yarn add x-easy

Simple start file

//index.js
const XEasy = require('x-easy')

// the default port is 3000
let app = new XEasy('myApp', { // Options for initialize app
    secure: false, // default
    port: 3000, // default
    disableNativeWebsocket: false, // default
    // optional, require (secure: true) to work
    disableSession: false // default
    keys: {
      key: './keyFile.key', // or './keyFile.pem'
      cert: './certFile.crt',  // or './certFile.pem'
      ca: ['./ca1.crt', './ca2.crt'] // chain certificate or (ca: './ca.crt') single certificate
    }
  })
  // loadModule can load <folder>/index.js, <folder>/index.json,
  // <file>.js and <file>.json
  // file to load data
  .loadModule('data') // json file
  // file to default settings
  .loadModule('config') // js file
  // file for route settings
  .loadModule('routes') // js file
  // file for websocket settings
  .loadModule('websocket') // js file
  // method for creating channel between processes
  .joinProcessListener('chat')
  // start children process
  /* 'auto' sets automatic scanning for the number of processor
  ** cores and creates processes for each of them
  ** .start('auto') with 6 cores start 6 process
  **
  ** negative numbers subtract from the number of colors to
  ** generate the number of processes that at least 1
  ** .start(-2) with 6 cores start 4 process*/
  .start(/* number of process, default is 1*/)

The loadModule function is used to instantiate objects in the application by passing it in the constructor.

//data.json
{
  'data': 'something else',
  'viewEngine': 'pug',
  'viewEnginePath': 'pages'
}
//config.js
module.exports = class Config {
  constructor(app, modules) {
    // event called when the process is exiting
    app.callStop = () => {
      //... stopping worker
    }

    // method that receives messages from channels between processes
    app.addProcessMessageListener((channel, data) => {
      // ...message processing
      // channel attribute refers to the channel name or application name
    })

    // for password protection, this method transforms the hashed data
    app.setParameterHash('pwd')
    app.setParameterHash(
      'password',
      /* optional */ {
        seed: 'secret', // default is ''
        loop: 5 // default is 1
      }
    )
    // children properties can also be digested
    app.setParameterHash('user.password')

    // method to take a public folder
    app.setPublic('public')
    // if there are subfolders
    // app.setPublic('public/images')

    // page rendering system
    app.setViewEngine(modules.data.viewEngine)
    app.setViewPath(modules.data.viewEnginePath)

    // permission for accept upload of files
    app.addUploadURLPermission('/upload')
  }
}
//routes.js
module.exports = class Routes {
  constructor(app, modules) {
    app.get('/', function(req, res) {
      // Sending messages to all registered processes on this channel
      app.sendProcessMessage(
        'chat',
        { foo: 'foo' } /* worker id 'app.worker.id' */
      ) // the message has to be an object

      // Sending messages to all  processes on this application
      app.sendAppProcessMessage({ foo: 'foo' } /* worker id 'app.worker.id' */) // the message has to be an object too

      res.end('test ok - ' + req.query.test)
    })

    app.post('/upload', (req, res) => {
      // ... file upload process
      // req.files.<form name>.name - file name
      // req.files.<form name>.encoding - file encoding
      // req.files.<form name>.mimetype - file mimetype
      // req.files.<form name>.data - Buffer bytes

      res.end('file upload success')
    })
  }
}
//websocket.js
const URL = require('url')

module.exports = class WebSocket {
  constructor(app, modules) {
    app.webSocket.on('connection', (conn, req) => {
      let data = URL.parse(req.url, true)

      if(data.pathname !== '/protocol') conn.terminate()
      else {
        conn.on('message', msg => {
          // ... process message
        }
        conn.on('close', () => {
          // ... process close connection
        })
      }
    })
  }
}

Constant Variables

app.server // http server object
app.app // express application instance
app.name // application name entered in the constructor
app.worker // child process control object

Keywords

xeasy

FAQs

Package last updated on 11 Mar 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