My previous post showed you how to install NodeJS, now we need to be able to do something with it. What better than a Hello World webpage?
Before we do that, I think we should take a step back and understand what NodeJS is. Those you have been following web development for a few years may probably come across terms such as MEAN and LAMP stack. These ‘stacks’ are basically the technology components to allow programmers to create applications that serve on the internet. MEAN short for Mongo, Express, Angular and NodeJS. LAMP is a lot older and means Linux, Apache, MySQL and PHP (or Perl or Python).
Make sure you have Node installed (see my previous post) then start by creating your project directory and initiate a npm project, this can be done on a linux machine with the following commands:
You should now have a npm project with all the default folder structure (packages.json and node_modules folder). Next you will need to install Express as a dependency. The command below will install Express and add this to you packages.json file:
npm install express --save
var express = require('express')
You should now be able to run from the terminal by using the command below:
If this loads correctly, you should be able to open a browser to url
localhost:3000 and see ‘Hello World’.