Join chat

Starting Logux Server Project

In this guide, we will create the Logux server for the simplest case:

If you want to use another language for the server read Logux Proxy page.

Creating the Project

Install Node.js (version 10.0 or later).

Create a directory for a project. We will use server-logux name.

mkdir server-logux
cd server-logux

Create package.json with:

{
  "name": "server-logux",
  "private": true,
  "scripts": {
    "start": "node index.js"
  }
}

Install Logux Server:

  • npm i @logux/server

    Create index.js with:

    const { Server } = require('@logux/server')
    
    const server = new Server(
      Server.loadOptions(process, {
        subprotocol: '1.0.0',
        supports: '1.x',
        root: __dirname
      })
    )
    
    server.auth((userId, token) => {
      // Allow only local users until we will have a proper authentication
      return process.env.NODE_ENV === 'development'
    })
    
    server.listen()

    The simple Logux server is ready. You can start it with:

  • npm start

    To stop the server press Command+. on Mac OS X and Ctrl+C on Linux and Windows.

    Database

    Logux Server supports any database. We will use PostgreSQL only as an example.

    Install PostgreSQL.

    Install PostgreSQL tools for Node.js:

  • npm i dotenv pg-promise node-pg-migrate pg

    Start database depends on your environment.

    Create database. Use this advice if you will have role does not exist error.

    createdb server-logux

    Create .env file with URL to your database. Put this file to .gitignore.

    DATABASE_URL=postgres://localhost/server-logux

    Create new migration for database schema:

    npx node-pg-migrate create create_users

    Open generated file from migrations/ and create users table:

    exports.up = pgm => {
      pgm.createTable('users', {
        id: 'id',
        email: { type: 'varchar', notNull: true, unique: true },
        password: { type: 'varchar', notNull: true }
      })
    }
    
    exports.down = pgm => {
      pgm.dropTable('users')
    }

    Run migration:

    npx node-pg-migrate up

    Connect to the database in index.js:

    const { Server } = require('@logux/server')
    const pg = require('pg-promise')
    
    const server = new Server(
      Server.loadOptions(process, {
        subprotocol: '1.0.0',
        supports: '1.x',
        root: __dirname
      })
    )
    
    let db = pg()(process.env.DATABASE_URL)

    Look at Node.js API to see what you can do next.