Tutorials

Setting up Your JavaScript Environment

Updated: July 26, 2017 Page Information Edit on GitHub

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);
      });
    
  2. Save and run:

    $ node usersGroups.js
    

22 days ago manonlumeau NXDOC-1276
10 months ago Solen Guitter 39
10 months ago Solen Guitter 38
a year ago Solen Guitter 37
a year ago Arnaud Kervern 36
a year ago Solen Guitter 35
a year ago Solen Guitter 34
a year ago Solen Guitter 33
a year ago Solen Guitter 32
a year ago Solen Guitter 31 | Fix JS
a year ago Bertrand Chauvin 30 | Fix code
a year ago Arnaud Kervern 29
a year ago Solen Guitter 28
a year ago Bertrand Chauvin 27 | Alicia and Sarah should be part of sales group
a year ago Solen Guitter 26
a year ago Bertrand Chauvin 25 | Add password for users
a year ago Bertrand Chauvin 24 | Added the managers groups creation
a year ago Solen Guitter 23 | Add learn more
a year ago Bertrand Chauvin 22 | Create several users
a year ago Solen Guitter 21
a year ago Bertrand Chauvin 20
a year ago Bertrand Chauvin 19 | Reverted from v. 17
a year ago Bertrand Chauvin 18 | Remove Alicia
a year ago Solen Guitter 17
a year ago Solen Guitter 16
a year ago Solen Guitter 15
a year ago Solen Guitter 14 | add password
a year ago Bertrand Chauvin 13 | the Nuxeo Platform => Nuxeo Platform
a year ago Bertrand Chauvin 12 | Fix json examples
a year ago Bertrand Chauvin 11 | use nuxeo js version 2 for 8.x
a year ago Solen Guitter 10
a year ago Solen Guitter 9
a year ago Solen Guitter 8
a year ago Solen Guitter 6
a year ago Solen Guitter 7
a year ago Solen Guitter 5
a year ago Solen Guitter 4
a year ago Solen Guitter 3
a year ago Solen Guitter 2
a year ago Solen Guitter 1
History: Created by Solen Guitter