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:
Open another instance of the terminal and enter the following
$ curl http://localhost:8080
Â
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 =)
Subreddit
Post Details
- Posted
- 7 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/webdev/comm...