Simple Node WebApp

This whole tutorial describes how to create and run a simple website and call a singular js file. In this tutorial I am just demoing how to get a page displaying on localhost. Nothing more.

FYI: I am using Windows at the moment for this tutorial but you can substitute Ubuntu in if you so chose.

We will require nodejs before you continue which you can get from here.

We use Express in this project demo. Express is a web framework.

Folder Structure:
  • You will need to create a folder called “testApp”.
  • Inside testApp create the following:
    • app.js
Node Init:

We need to go to our root directory testApp from command prompt and run the following order.

npm init
npm install express --save

Things to Note:

  • npm init: Creates package.json file
  • npm install –save: will save the install to package.json
  • Each package install went into a new directory called “node_modules”.
Setup app.js:
var express = require('express');
var app = express()

app.use(express.logger('dev'))

app.get('/', function (req, res) {
	res.send("hi");
	res.end();
})
app.listen(5000)

Things To Note:

  • var app = express(): Setups up our express app
  • res.use: is intended for binding middleware to your application
  • res.get: is used for routing.
  • res.send: sends data
  • res.end:Ends the response call.
  • You get find all the API reference here.
Run It!
node app.js

Your site is now running on http://localhost:5000. Notice all it says is “HI”. That is because this is just a demo to show you how to get going. Expand as needed.