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:
- Create a public repo in your GitHub account, choose
MIT
for the license, and add aREADME
file. - Follow the steps here to connect your Cloud9 and GitHub accounts.
- 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
- Under the
Choose a Template
field chooseNode.js
and then click theCreate workspace
button. - 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!");
- While still on the Cloud9 workspace, save the file by pressing
Command
+S
(Mac) orControl
+S
(Windows) on your keyboard. - 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 pressEnter
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
- (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
- Then, to install a package like Express AND update the
package.json
file, type in something like the example below:
npm install express --save
- (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
- Also install Nodemon and write it to your
package.json
file:
npm install nodemon --save
- 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
- Choose
Preview > Preview Running Application
drop-down at the top of your Cloud9 workspace. - A new tab will open that is a preview of your web page and says:
Hello World
- 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/ - 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
- Finally, double-check your GitHub repo to ensure that Cloud9 workspace edits were pushed there.