Blog

Setting Up a Basic Node.js Workspace on Cloud9
Posted on May 26, 2016 in Node.js by Matt Jennings

Cloud9 (c9.io) is an online service that can be used to quickly create websites in various languages and environments including Node.js, Ruby on Rails, PHP, and Python.

It’s possible to use this service to quickly create a Node.js AND view the live Node.js web page, all while using domains controlled by Cloud9 only. This means that the command line, Node.js web page, files, and everything else you need to quickly make a Node.js website live is available through website provided by Cloud9.

Below are steps to quickly set up a Node.js website using Cloud9:

  1. Create a public repo in your GitHub account, choose MIT for the license, and add a README file.
  2. Follow the steps here to connect your Cloud9 and GitHub accounts.
  3. Create a Cloud9 workspace and in the Clone from Git or Mercurial URL field enter your GitHub repo url, like the example below:
    https://github.com/yourGithubUsername/yourRepoName
  4. Under the Choose a Template field choose Node.js and then click the Create workspace button.
  5. Then your Cloud9 Node.js workspace will be created. On the root directory of this workspace create a server.js file with the code listed below:
    var express = require('express');
    var app = express();
     
    app.get('/', function (req, res) {
      res.send('Hello World');
    });
     
    var port = Number(process.env.PORT || 5000);
    app.listen(port);
    
    console.log("Blah!");
  6. While still on the Cloud9 workspace, save the file by pressing Command + S (Mac) or Control + S (Windows) on your keyboard.
  7. In the bash shell that is inside your Cloud9 workspace web page, type npm init to create a package.json file that will include your Node.js project settings. In the shell answer each question and press Enter on your keyboard like the examples below:
    name: node-test
    version: 0.0.1
    git repository: https://github.com/yourGithubUsername/yourRepoName
    keywords: Node.js, Express, Handlebars
    license: MIT
    entry point: server.js
    test command: nodemon server.js
    Is this ok? yes
  8. (OPTIONAL) To update your version Node, use the Node Version Manager which is installed by default. For example, to update to Node version 6.2.1 enter the command below:
    nvm install 6.2.1

    Then, to see what version of Node you are using enter:
    node -v

    Finally, to see all versions of Node available for use enter:
    nvm ls

  9. Then, to install a package like Express AND update the package.json file, type in something like the example below:
    npm install express --save
  10. (OPTIONAL) To install a package globally, navigate to your root directory and use the -g flag like the example below, which would install nodemon globally:
    npm install -g nodemon
  11. Also install Nodemon and write it to your package.json file:
    npm install nodemon --save
  12. Finally to run the Node app, and view it on a Cloud9 web page, in the bash on Cloud9 type the command below and press Enter on your keyboard:
    nodemon server.js
  13. Choose Preview > Preview Running Application drop-down at the top of your Cloud9 workspace.
  14. A new tab will open that is a preview of your web page and says:
    Hello World
  15. To see this web page in a new browser window choose the icon to the right of the Browser button in the preview tab. Then a new tab will open up. Delete everything after and inclusive of the ? mark to see the live web page on Cloud9 servers, like the example below:
    https://nodejs-poll-example-app-hollyw00d.c9users.io/
  16. After you are done with making edits to your Cloud9 workspace, enter the commands below to push them to your connected GitHub repo, pressing Enter on your keyboard after each command:
    git add -A
    git commit -m "edits made"
    git push
  17. Finally, double-check your GitHub repo to ensure that Cloud9 workspace edits were pushed there.

Leave a Reply

To Top ↑