Tutorials

Setting up Your JavaScript Environment

Updated: February 22, 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
    

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