hello mean-ish stack world

2017-04-05
programming/ javascript/ angular/ node

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).
Node basically is a JavaScript engine outside the browser, therefore allows you to run JS code to lot of fun things. The rest of the stack components will be covered in future blog posts.
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:

1
2
3
mkdir helloworld
cd helloworld
npm init -y

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:

1
npm install express --save

You default Node project will have been created with index.js as the entrypoint, you will need to update this file (or create if it doesn’t exist) with the following to pick up and run the JavaScript code with Express:

1
2
3
4
5
6
7
8
9
10
var express = require('express')
var app = express()

app.get('/', function (req, res) {
res.send('Hello World!')
})

app.listen(3000, function () {
console.log('Example app listening on port 3000!')
})

You should now be able to run from the terminal by using the command below:

1
node index.js

If this loads correctly, you should be able to open a browser to url localhost:3000 and see ‘Hello World’.