Express.js Introduction


expressjslogo

Wikipedia describes Express.js this way: “Express.js, or simply Express, is a web application framework for Node.js, released as free and open-source software under the MIT License. It is designed for building web applications and APIs.

Express is the de facto standard web server framework for Node.js. The original author, TJ Holowaychuk, described it as a Sinatra-inspired server, meaning that it is relatively minimal with many features available as plugins. Express is the back-end part of the MEAN stack, together with MongoDB database and AngularJS front-end framework.

Below is some JavaScript code that sends a message. ‘Express Works!’ to the localhost port 3000 in your browser. In a few minutes, after completing the exercises below, you will be able to open your browser for the URL type localhost:3000 and press enter. I got this code from a YouTube video called Express Tutorial by Derek Banas.

var express = require('express');
var app = express();
app.set('port', process.env.PORT || 3000);
app.get('/', function(req, res){
   res.send('Express Works!');
});
app.listen(app.get('port'), function(){
   console.log('Express started press Ctrl+C to terminate');
});

After Installation – Express Tutorial by Derek Banas

After you have installed node and express you are ready to begin. First create a directory on your computer where you want to store the files of this video tutorial. Next, open a command prompt and navigate to that directory. Type node -v to verify that node was installed correctly. You should see a version number.

Type npm init.

Press ^C at any time to quit.
name: (tutorial)
version: (1.0.0)
description: Express Tutorial
entry point: (index.js) expresstut.js
test command:
git repository:
keywords:
author:
license: (ISC)
About to write to C:\Data\Portfolio\Express\tutorial\package.json:

{
  "name": "tutorial",
  "version": "1.0.0",
  "description": "Express Tutorial",
  "main": "expresstut.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}


Is this ok? (yes) yes

Open your package.json file and make the following changes. You should add “private”: true, to that file so that it looks like this:

{
  "name": "tutorial",
  "version": "1.0.0",
  "private": true,
  "description": "Express Tutorial",
  "main": "expresstut.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

At your command prompt, type npm install –save express and hit Enter.

At the command prompt type npm install –save express-handlebars and press Enter.

Starting the Express Server

Open a command prompt. Change the directory to the location of your js file(s). If the name of your js file is mysite.js, then at the command prompt type node mysite.js. You shoule see: Express started press Ctrl+C to terminate. Go to your browser and type localhost:3000. You should see the message Express Works!. Stop the server with Ctrl+C. Edit the js file. Change the line

res.send('Express Works!');

to something else such as

res.send('Hello World. Express Works!');

and re-start the server. To restart it just go to your command line and type Ctrl+C and then start the server by typing node and your js filename as discussed. You should now see your new message at localhost:3000 in the browser.

Leave a comment

Your email address will not be published. Required fields are marked *