Coming soon - Get a detailed view of why an account is flagged as spam!
view details
21
Getting started with Node.js in Ubuntu for ABSOLUTE BEGINNERS (part 1)
Post Body

I've been coding in the .NET world for a while now, and I've really been wanting to branch out. I finally took the plunge and started on something COMPLETELY different. I have very little experience with Linux, and I wanted to completely break free from Microsoft (this is following a very frustrating ordeal with M$ support).

This post is largely derived from this guide but has been updated to use newer versions of everything. (From NodeJS v0.4.4 to NodeJS v8.1.2). The "application" does nothing more than return a "hello world" message, but this guide will focus mainly on setting up your environment and starting an http server. Part 2 will delve into more of the actual coding.

I have chosen to install Ubuntu into a virtual machine which is run from within Windows, but you may opt to install it directly to your hard drive if you would like to dual-boot or run Linux exclusively. Also I am using VS Code because I hear it's pretty good, and I want to retain some familiarity with what I'm doing, there are lots of good editors out there that you can use - but that discussion goes beyond the scope of this post.

 

Downloads:

 

Install VirtualBox

  • Create new instance, keep everything to defaults except for RAM usage (your choice)
  • With your new virtual machine selected in the VirtualBox Manager:
    • In the "Storage" section
    • click where it says "[Optical Drive] Empty"
    • select "Choose Disk Image"
    • find and select your Ubuntu ISO
  • Start your new VM
  • Follow the wizard to install Ubuntu

 

Installing NodeJS

  • Open Terminal (ctrl-alt-T)
  • In Terminal:

    $ wget https://nodejs.org/dist/v8.1.2/node-v8.1.2.tar.gz
    $ tar -xzf node-v8.1.2.tar.gz
    $ cd node-v8.1.2.tar.gz
    $ ./configure
    $ sudo make isntall
    
  • If this is a fresh Ubuntu installation, you will probably have to install common web development dependencies:

    $ apt-get -y install build-essential
    
    • be patient, this took over half an hour to complete on my machine

 

Installing VSCode

  • https://code.visualstudio.com/docs/setup/linux
  • click the ".deb package" link
  • "Save File"
  • open up your file manager, navigate to the home/downloads directory, and rename (f2) the .deb package to something simpler. I used "vsCode.deb"
  • In Terminal:

    $ cd Downloads
    $ dir   #(just to make sure you're in the right spot.  You should see vsCode.deb)
    $ sudo dpkg -i vsCode.deb
    $ sudo apt-get install -f
    $ sudo apt-get update
    $ sudo apt-get install code
    
  • VS Code should be installed at this point. You can open it through "Dash" (Ubuntu equivalent of the Start Menu). Open Dash by pressing the "Super" button on your keyboard (windows button).

  • Alternatively, you can open VS Code by typing "code" in the terminal.

    $ code
    

 

Writing our first application

  • Open the explorer (first icon on the left side)
  • You should see a message that says "You have not yet opened a folder"
  • Click "Open Folder"
  • Go to "Documents"
  • Click "Create Folder" name it "Workspace"
  • Click "Create Folder" name it "HelloWorld"
  • Click "OK"
  • Click "New File" and name it HelloWorld.js
  • Enter the following into your new document, then save it:

    console.log('hello world'); 
    

 

Test our application

  • Open Terminal
  • Navigate to the HelloWorld folder

    $ cd Documents/Workspace/HelloWorld
    
  • Run the javascript file using Node.js

    $ node HelloWorld.js    
    
  • If "hello world" is outputted into the terminal then everything is installed correctly and your application is working!

 

Making our application into something a little more substantive

  • Switch back over to VS Code
  • Change HelloWorld.js to the following and save:

    var http = require('http');
    var server = http.createServer(function(req, res){
        res.writeHead(200);
        res.end('Hello Http \n');
    });
    server.listen(8080);
    
  • Save your changes

 

Testing our newly created application

  • Run your js file through Node in the Terminal again

    $ node HelloWorld.js
    
  • You will notice that the terminal does not respond with anything, instead your cursor is now on a blank line.

  • Open a web browser and navigate to:

    • http://localhost:8080
    • The web page should say "Hello Http"
  • Alternatively, we can test the application through a terminal instance using curl:

 

Conclusion:

It may not look by much, but it's the first starting point. If this is working for you it means you have all the basic prerequisites complete and you can start developing. In Part 2 we will be creating a new project that does something a little more interesting than just returning a "hello world" message.

I hope that someone finds this guide useful. If anything is unclear, inaccurate, outdated or poorly worded, please let me know and I'll revise it. Thanks for reading =)

Author
Account Strength
100%
Account Age
13 years
Verified Email
Yes
Verified Flair
No
Total Karma
528,808
Link Karma
33,830
Comment Karma
493,790
Profile updated: 6 days ago
Posts updated: 10 months ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
7 years ago