Pre-requisites:
- NodeJS (this includes the npm package manager as well)
- A Github account and therefore Git DVCS (Distribruted Version Control System)
To begin with lets make a new directory for the project. I am using C:\code\nodestart
Create a file called
package.json
:
{
"name": "nodestart"
,
"version": "0.0.1"
,
"dependencies":
{
"express": "latest"
}
}
Then change directory to the location of your project in Command Prompt, and run npm install
cd C:\code\nodestart
npm install
So at this point we have everything we need to add further packages in the future. Lets add a new server.js file in our directory as well.
console.log("Hello World");
To run this, go back to the command line and type:
node server.js
You are treated with a "server running at http://127.0.0.1:1337/" message, paste this into your browser and voilà! Basic node achieved! Hazzah!
Now what if, for some strange reason, we wanted to publish our fine work? I mean everyone needs a useless site. No, well lets do it anyway!
First we need to use some slightly more sophisticated code:
var http = require('http') var port = process.env.PORT || 1337; http.createServer(function(req,res){ res.writeHead(200, {'Content-Type':'text/plain'}); res.end("hello world"); }).listen(port);
Now we need to initialise git to make our nodestart project a working git repository. Go to Git Bash (downloaded from git hub) or the command line, cd to the project directory and type:
git init
Notice that this will add a .git folder to the root of your project. We must also add a new file in the root called .gitignore. Add the following lines:
npm-debug.log
node_modules
node_modules
This is so that you do not add all the node library files to the solution. Now it is safe to type:
git add .
git commit -m 'Initial commit'
It would be nice to setup ourselves properly in terms of our credentials and user names - (https://help.github.com/articles/set-up-git). Nobody likes annonymous commits... there is no-one to blame!
Also rather than keep typing in your credentials you may want to setup an SSH key. This is very much optional and takes a little time but it makes pushing to source control far less tedious. If you do decide to do this remember to change the clone URL to the SSH one in github!
Now all we need to do is set the origin and push to our github repository.
git remote set-url origin git@github.com/username/nodestart
git push -u origin master
git push -u origin master
I am going to use Windows Azure to host this application. You can sign up to get an account with a free trial. Afterwards you can keep the server in free mode and keep auto scale off to guarantee that it will not cost you any money.
Azure has the ability to "integrate source control". This can be done from the UI as shown below or you can use the Azure command line tools through Windows PowerShell.
It's really neat and means that you can push directly to Azure without using a web based repository host like Github or Bitbucket. For more information on this method check out Scott Hanselmans post on the subject.
Now back in the Azure console I am going to choose integrate source code as before but this time choose "github" rather than "local git repository".
Select your repository and treat master as your CI branch and there you go! Success!
Additional: Windows Azure can cope with npm but what about bower? I found a solution for this problem here: http://gregtrowbridge.com/deploying-a-bower-dependent-node-app-on-windows-azure/
The only problem was that it relies on a deploy.sh file being produced when running "azure site deploymentscript --node". Instead my solution produced a batch file with Kudu code. To cut a long painful story short I was able to resolve this with the following code:
:: 4. Install bower packages
if EXIST "%DEPLOYMENT_TARGET%\bower.json" (
pushd "%DEPLOYMENT_TARGET%"
call :ExecuteCmd !NPM_CMD! install -g bower
pushd "%DEPLOYMENT_TARGET%\public\libs"
call :ExecuteCmd bower install
IF !ERRORLEVEL! NEQ 0 goto error
popd
)
Sources and references: http://www.nodebeginner.org/#javascript-and-you, http://kunkle.org/nodejs-explained-pres/#/example-http-server, http://howtonode.org/, http://nodejs.org/api/http.html#http_httpif EXIST "%DEPLOYMENT_TARGET%\bower.json" (
pushd "%DEPLOYMENT_TARGET%"
call :ExecuteCmd !NPM_CMD! install -g bower
pushd "%DEPLOYMENT_TARGET%\public\libs"
call :ExecuteCmd bower install
IF !ERRORLEVEL! NEQ 0 goto error
popd
)
No comments:
Post a Comment