{"__v":2,"_id":"54d039c3f530ca0d0005396a","category":{"__v":19,"_id":"54cfba19bba1a023008741b9","project":"54cf411f9d09bb0d00a17a1c","version":"54cf411f9d09bb0d00a17a1f","pages":["54d0351994f7980d0081b848","54d03542c795960d009f4219","54d0357d94f7980d0081b84b","54d039c3f530ca0d0005396a","54d039e394f7980d0081b859","54d03a1294f7980d0081b85b","54d03a3bf530ca0d0005396e","54d03a9294f7980d0081b85e","54d03abdc795960d009f4220","54d03ade94f7980d0081b860","54d03b02f530ca0d00053970","54d03b3794f7980d0081b862","54d03b69f530ca0d00053973","54d03b8994f7980d0081b864","54d03bb1c795960d009f4222","54d8f49e6a09052100a6b60e","54ec495d7ab3172d00c01a92","54ec6f2ec0c0ba1700386fa6","54f6f3f852174719008f6168"],"createdAt":"2015-02-02T17:55:37.930Z","from_sync":false,"order":4,"slug":"running-and-debugging","title":"Running and Debugging"},"project":"54cf411f9d09bb0d00a17a1c","user":"54cfa8c8a8a4fd0d00b7fd1c","version":{"__v":12,"_id":"54cf411f9d09bb0d00a17a1f","project":"54cf411f9d09bb0d00a17a1c","createdAt":"2015-02-02T09:19:27.656Z","releaseDate":"2015-02-02T09:19:27.656Z","categories":["54cf41209d09bb0d00a17a20","54cfb99bbba1a023008741af","54cfb9a65ff7e617002bbd7f","54cfba03bba1a023008741b6","54cfba0ebba1a023008741b8","54cfba19bba1a023008741b9","54cfba29bba1a023008741bb","54cfba345ff7e617002bbd87","54cfba3ebba1a023008741bc","54cfba473995cf0d0006f6f0","54e371b18ef7552300409bf2","54e37aa5e887c50d005ef629"],"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"1.0.0","version":"1.0"},"updates":[],"createdAt":"2015-02-03T03:00:19.287Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"auth":"required","params":[],"url":""},"order":2,"body":"Cloud9 IDE was built on top of the Node.js platform, and as such, you have full access to the `node` runtime. Currently, we support running both version v0.6.x and v0.8.x.\n\nIn this section, we'll walk you through the creation of a simple Hello World program. To get started, you'll first need to create a (GitHub or Mercurial) project. If you need a refresher on how to do this, please refer to [Creating a New Workspace](creating_new_workspace.html).\n\n## A Simple Node.js HTTP Server\n\nOnce you're in Cloud9, create a new file called `server.js`. Type the following code in the file:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"var http = require('http');\\nhttp.createServer(function (req, res) {\\n    res.writeHead(200, {'Content-Type': 'text/plain'});\\n    res.end('Hello World\\\\n');\\n}).listen(process.env.PORT, process.env.IP);\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\nThis is a Node.js HTTP server. It returns a simple \"Hello World\" page every time you access the page. In short, you are creating an HTTP server with a callback function that is called for each request.\n\nIn the callback function, you create a response with a status code of `200` (indicating that the request was fulfilled successfully) and the message \"Hello World\". Finally, you specify which port and IP address the server runs on. When Cloud9 IDE runs servers, you set and retrieve the IP address and port number with the `process.env.IP` and `process.env.PORT` variables.\n\nTo see your application in action, click on the Run button, and the output pane will show a link created for your project. If you click on that link, you should see your \"Hello World\" application open up in a new preview pane. In order to access your running server from another tab, you can access it at: http://<workspacename>-<username>.c9.io  \n\n### NVM Support\n\nWe have rudimentary support for `nvm`, that allows you only to switch between Node.js versions 0.6.21 and 0.8.x. \n\nTo use `nvm`, open [the terminal](./terminal.html) and type:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"# to switch to version 0.6.21\\nnvm use v0.67 \\n\\n# to switch to the latest 0.8.x\\nnvm use v0.8 \",\n      \"language\": \"text\"\n    }\n  ]\n}\n[/block]","excerpt":"","slug":"writing-a-nodejs-app","type":"basic","title":"Writing a Node.js App"}

Writing a Node.js App


Cloud9 IDE was built on top of the Node.js platform, and as such, you have full access to the `node` runtime. Currently, we support running both version v0.6.x and v0.8.x. In this section, we'll walk you through the creation of a simple Hello World program. To get started, you'll first need to create a (GitHub or Mercurial) project. If you need a refresher on how to do this, please refer to [Creating a New Workspace](creating_new_workspace.html). ## A Simple Node.js HTTP Server Once you're in Cloud9, create a new file called `server.js`. Type the following code in the file: [block:code] { "codes": [ { "code": "var http = require('http');\nhttp.createServer(function (req, res) {\n res.writeHead(200, {'Content-Type': 'text/plain'});\n res.end('Hello World\\n');\n}).listen(process.env.PORT, process.env.IP);", "language": "javascript" } ] } [/block] This is a Node.js HTTP server. It returns a simple "Hello World" page every time you access the page. In short, you are creating an HTTP server with a callback function that is called for each request. In the callback function, you create a response with a status code of `200` (indicating that the request was fulfilled successfully) and the message "Hello World". Finally, you specify which port and IP address the server runs on. When Cloud9 IDE runs servers, you set and retrieve the IP address and port number with the `process.env.IP` and `process.env.PORT` variables. To see your application in action, click on the Run button, and the output pane will show a link created for your project. If you click on that link, you should see your "Hello World" application open up in a new preview pane. In order to access your running server from another tab, you can access it at: http://<workspacename>-<username>.c9.io ### NVM Support We have rudimentary support for `nvm`, that allows you only to switch between Node.js versions 0.6.21 and 0.8.x. To use `nvm`, open [the terminal](./terminal.html) and type: [block:code] { "codes": [ { "code": "# to switch to version 0.6.21\nnvm use v0.67 \n\n# to switch to the latest 0.8.x\nnvm use v0.8 ", "language": "text" } ] } [/block]