Skip to content

KDawg/nodejs_sample_webapp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Simple "Hello World" Web Server in NodeJS

This source code repo is a sample app showing how to do the simplest possible web server app in NodeJS.

It's mean to be a learning exercise, and demonstrate this amazing web technology. JavaScript is the primary language of web frontend, and it can be on the backend as well.

You can be a fullstack web developer by knowing JavaScript well.

Setting-up the Sample App

There are a few one-time only steps for installing pre-requisites listed below.

Installing NodeJS

This sample webapp is written using NodeJS. Running it means having the NodeJS run-time setup on your computer. Never installed NodeJS? No problem. Refer to the Node Foundation and find an installer matching your O/S.

Cloning with Git

Take a copy of the source code from the repo on GitHub. Looking at this sample app repo you’ll find the green button labeled “clone”. Clicking it reveals the URL you’ll use to git clone, which is how source code is first copied down from the server to your computer.

I'm assuming you're familiar with git revision control. If you haven't used it much there's a ton of resources online that you can search for. It's worth getting comfortable with it!

Installing Project Dependencies

To summarize.

  • You’ve installed NodeJS
  • You've pulled down a copy of the source code

Now install the app’s dependencies. Run this command in your local copy of the repo:

npm install

This tells npm (Node Package Manager) to pull down copies of the open-source libraries this app uses. Could take a few minutes.

Running the Sample App

Your copy of the app is ready to run. Run this command in your local copy of the repo:

npm start

Open the WebPage

Launch your favorite web browser and enter the URL your local web server is running on with one of the two routes that it offers:

http://127.0.0.1:3000/
http://127.0.0.1:3000/hello

Each is slightly different. Why? Simply show how that's done. Most every webserver you've ever seen has more than one route.

Code Reading

Start at the beginning, app.js, and notice how the NodeJS web server is spun up and gets working. This function SetupRoutes is particularly interesting because it calls helper functions setting up the GET action response for the default route.

Request handlers are implemented in site.js. Each sends back an HTML response generated by rendering through Handlebars template engine for ExpressJS.

Look at main.html seeing how to setup a page for the template engine to work.

ExpressJS is a library for setting up a webserver. NodeJS is simply a JavaScript run-time interpreter. We want to use it for making a webserver and Express is key for setting up HTTP.

You'll see the app's webpage has a little bit of style. You'll notice styles are defined in main.css. In fact public is a special folder for static resources served up by pages. You'll see that established in the SetupParsingPublic function in app.js.

As you read through hello.html you'll notice it's something special. It takes data from the function rendering it and substitutes that data inline using the Handlebars Expressions syntax like this: {{whenRendered}}.

Another Way to Run

I use this during development to run the app:

npm run watch

This script leverages a library called nodemon and tightens up the design -> build -> test loop by watching for source code file changes. Nodemon restarts the app while you're editing and saving files.

Automation tools like this makes software development a little bit easier.

Where is that script defined? Because it's related to npm it'll be found in package.json. The definition file that makes npm work for your project.

Accelerate Your Learning

Use this sample app to accelerate your learning. Play around in it. Change things. Make it your own. It should be enough to get your inventive self going strong.

Be sure to reach out to me on Twitter and tell me when you've done something cool.

Good luck, have fun!

About

A simple webapp written in NodeJS for you to learn how this fantastic system works!

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published