Getting Started

Setting up Your JavaScript Environment

Updated: November 15, 2024

This how-to is about installing Node.js and the Nuxeo JavaScript Client, and creating a few users in your Nuxeo Platform instance that you will need later in the tutorial.

Nuxeo JavaScript Client is a library for the Nuxeo Automation and REST API. The library can work in a browser or in Node.js using the same API. In this tutorial you will use Node.js to discover the features of Nuxeo Platform. If you want an overview of the JavaScript client, read the Nuxeo JavaScript Client page.

Setting up the JavaScript Environment

In this section, you will set up your work environment: install Node.js on your system, install Nuxeo JavaScript Client and prepare a folder on your system in which you will save your JavaScript files.

  1. Install Node.js. The minimum required version is v6.5.0.

    If you previously installed Node.js, you can check your version with the following command:

    $ node --version
    
  2. Create a folder called nuxeo_tutorial on your computer.
  3. Install the JavaScript Client package in that folder.

    1. Open a terminal and go that folder.

      $ cd PATH/TO/nuxeo_tutorial
      
    2. Initiate the Node module and follow the steps displayed to generate the module package.json file.

      $ npm init
      
    3. Use npm to install the Nuxeo JavaScript Client package.

      $ npm install [email protected] --save
      

      A node_modules folder is now available in nuxeo_tutorial.

    You are now set: you have a folder in which you will save your JavaScript files, and from which the Nuxeo JavaScript client is available.

Creating Users and Groups

The goal is to create the sales and managers group, as well as three users in Nuxeo Platform:

  • John Doe - a regular user — username and password: jdoe
  • Alicia Fraser - part of the sales team — username and password: afraser
  • Sarah Connor - sales manager — username and password: sconnor

To create the users:

  1. Using your favorite text editor or IDE, create a file called usersGroups.js with the following content:

    #!/usr/bin/env node
    const Nuxeo = require('nuxeo');
    const nuxeo = new Nuxeo({
      auth: {
        method: 'basic',
        username: 'Administrator',
        password: 'Administrator'
      }
    });
    
    const groups = [
      // sales group
      {
        groupname: 'sales',
        grouplabel: 'Sales'
      },
      // managers group
      {
        groupname: 'managers',
        grouplabel: 'Managers'
      }
    ];
    
    // Define the users
    const users = [{
      properties: {
        'entity-type': 'user',
        username: 'jdoe',
        password: 'jdoe',
        firstName: 'John',
        lastName: 'Doe',
        company: 'Big Corp',
        email: '[email protected]',
        groups: ['members']
      }
    }, {
      properties: {
        'entity-type': 'user',
        username: 'afraser',
        password: 'afraser',
        firstName: 'Alicia',
        lastName: 'Fraser',
        company: 'Big Corp',
        email: '[email protected]',
        groups: ['members', 'sales']
      }
    }, {
      properties: {
        'entity-type': 'user',
        username: 'sconnor',
        password: 'sconnor',
        firstName: 'Sarah',
        lastName: 'Connor',
        company: 'Big Corp',
        email: '[email protected]',
        groups: ['members', 'sales', 'managers']
      }
    }];
    
    Promise.all(groups.map((group) => {
        return nuxeo.groups().create(group);
      }))
      .then(values => {
        values.forEach(group => {
          console.log(`Group ${group.groupname} has been created.`);
        });
      })
      .then(() => {
        return Promise.all(users.map(function(user) {
            return nuxeo.users().create(user);
          }));
      })
      .then(values => {
        values.forEach(user => {
          // Display the usernames that have been created
          console.log(`User ${user.properties.username} has been created.`);
        });
      })
      .catch(error => {
        console.log('Apologies, an error occurred.');
        console.log(error);
        if (error.response) {
          error.response.json().then(json => console.log(json));
        }
      });
    
  1. Save and run:

    $ node usersGroups.js