In this guide, we will create the Logux server for the simplest case:
- You have simple HTTP server to serve static HTML, CSS, and JS files.
- You use Logux Redux or Logux Vuex on the client side.
- Logux server do most of back-end business logic.
- You write Logux server on Node.js.
If you want to use another language for the server read Logux Proxy page.
Creating the Project
Install Node.js (version 12.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,
"type": "module",
"scripts": {
"start": "node index.js"
}
}
Install Logux Server:
npm i @logux/server
pnpm add @logux/server
yarn add @logux/server
Create index.js
with:
import { Server } from '@logux/server'
const server = new Server(
Server.loadOptions(process, {
subprotocol: '1.0.0',
supports: '1.x',
fileUrl: import.meta.url
})
)
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
pnpm start
yarn 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 tools for Node.js:
npm i dotenv pg-promise node-pg-migrate pg
pnpm add dotenv pg-promise node-pg-migrate pg
yarn add 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.