Skip to content
VinceZK edited this page Apr 4, 2020 · 2 revisions

Experience Yourself

You can access the hosted websites to experience yourself first.

Setup Your Own Environment

  1. Install JOR to your node project:

     $ npm install json-on-relations --save
  2. Create the database in MySQL:

    After you installed MySQL, copy file "node_modules/json-on-relations/MDB.sql" to your sql console and execute. The script will create database 'MDB' which contains all the tables and the data.

    Please also create a DB user with full permission on data definition and manipulation. The DB user will be used for JOR to connect with MySQL.

  3. In your NodeJS project:

    Copy the folder node_modules/json-on-relations/dist and its belongings to your project root.

    Create server.js in your project root with following codes:

    const express = require('express');
    const app = express();
    
    // We don't want to serve sessions for static resources
    const path = require('path');
    app.use(express.static(path.join(__dirname, 'dist/jor')));
    
    const cors = require('cors'); // Allow cross site requests
    app.use(cors());
    
    app.use(require('body-parser').json());
    const compress = require('compression');
    app.use(compress());
    
    // Routing
    const routes = require('json-on-relations').Routes;
    app.use('/', routes);
    // The index page as an entry point
    app.route('*').get( (req, res) => {   
      res.sendFile(path.join(__dirname, '../dist/jor/index.html'));
    });
    
    process.on('SIGINT',function(){
      console.log("Closing.....");
      process.exit()
    });
    
    const entityDB = require('json-on-relations').EntityDB;
    entityDB.setConnPool('mysql', { // Set the connection pool to your mysql DB.
                                    // Currently, we only support mysql.
                                    connectionLimit : 10,
                                    host: 'localhost',  // To be replaced by your DB host
                                    user: 'nodejs',     // To be replaced by your own DB user
                                    password: 'nodejs', // To be replaced by your own DB password
                                    database: 'MDB',
                                    createDatabaseTable: true,
                                    multipleStatements: true,
                                    dateStrings: true,
                                    port: 3306           // replaced by your DB port.
                                  });
    app.listen(3000, () => console.log('Example app listening on port 3000!'));

    You should also install the involved packages: express, path, cors, body-parse, and compression.

  4. Start the server:

     $ node server.js
  5. You should now be able to open the following links: