🌍 Basic Web App Server

❔ What is Node.js?

Node.js is a JavaScript run-time environment which allows code to be used for server-side scripting, where scripts are run by the server before being delivered to the client. Most notably, Node.js utilizes an asynchronous approach where requests are continuously handled and simultaneously processed. This allows it to process requests with low latency and high throughput. Node can generate page content, read and write files, collect form data, and modify databases. Node.js is one of the most powerful tools one can use to develop web applications.Β 

πŸ–₯️ Server Web App Setup using Node.js:

Note: This tutorial assumes that you already have at least one html page created for your website.

  1. Alternatively if you have terminal, or the ubuntu subsystem for windows you can use: sudo apt-get update, sudo apt-get install nodejs, and sudo apt-get install npm (Note: npm is Nodes’ package manager).

  1. Set up the app.js file in your webapp directory:Β 
  1. Where you set this up is arbitrary, however, you should memorize where your index.html file is so that you can let node know which html files it should displays upon startup.
  1. Create the app.js file:
var port = process.env.PORT || 8888; //sets local server port to 8888
var express = require('express'); // Express web server framework
var redirect_uri = 'http://localhost:8888'; // Your redirect uri in case you are using apis
var app = express();Β 
app.use(express.static(__dirname + '/public')) //this directs the app to the directory with the html file, __dirname is the directory of the app.js file, in this case my index.html is in a folder called public that is in the same directory, if your index.html is in the same directory as app.js remove the β€œ+ β€˜/public/’”
console.log('Listening on 8888'); //prints to the console
app.listen(port, function() {}); //starts the server, alternatively you can use app.listen(port)

  1. Start the server using git bash/node.js command prompt or terminal.
  1. If you installed node from the website you should have a node.js app command prompt, if you used sudo-apt get then it should simply be installed on your terminal and you can check this by running nodejs -v.
  1. Go to your app.js directory using either the node.js app command prompt or terminal depending on how you installed it, and run node app.js. If you used the same port as the one in the coding snippet above and node app.js doesn’t error out you should be able to type localhost:8888 on any browser and you should see your running website!

Note

If you want node to automatically refresh whenever you edit files (on node normally you can change everything except for the app.js file and simply refresh your browser to see changes) use npm install -g nodemon and run the server using nodemon app.js.

πŸ’‘ Project Ideas

  1. Build a chat app with Socket.IO, a real time messaging module that can be plugged into virtually anything.Β 
  1. Improve the chat application WebRTC, an API that allows browsers and mobile apps access to Real Time Communication.Β 
  1. Build a desktop app with Electron, a framework that makes it easy to build apps with HTML,CSS, and JavaScript.Β 

πŸ“š Further Resources

  1. Express, a web framework for Node.js. Adds a small layer of web application utility above Node.js.
  1. The Node Beginner Book. A guide to developing applications with Node.js.
  1. Getting Started with Node.js Teaches how to deploy Node apps to Heroku. Β 
  1. Node.js Tutorials. A series of courses targeted towards Node beginners.Β