Forums >> Programming >> Open Source >>
My First Express App




Posted:
bvstone

My First Express App

 
My First Express App

Express comes with an application generator that you can use to create the skeleton for an application.  Before we can use it, though, it needs to be installed (separate from Express itself).  That is done with the following command:

npm install express-generator -g

Once this is complete we can use the express command to create the skeleton of our application.  For this we chose to simply list printers available from our Google Cloud Print application.  (We actually have a file in a library named G4GPRTPF in our GreenTools for Google Apps library that contains this).

express listPrinters

This command produces the following results showing that the base skeleton for the application is created:

bash-4.2$ express listPrinters

   create : listPrinters
   create : listPrinters/package.json
   create : listPrinters/app.js
   create : listPrinters/public
   create : listPrinters/routes
   create : listPrinters/routes/index.js
   create : listPrinters/routes/users.js
   create : listPrinters/public/images
   create : listPrinters/views
   create : listPrinters/views/index.jade
   create : listPrinters/views/layout.jade
   create : listPrinters/views/error.jade
   create : listPrinters/public/javascripts
   create : listPrinters/public/stylesheets
   create : listPrinters/public/stylesheets/style.css
   create : listPrinters/bin
   create : listPrinters/bin/www

   install dependencies:
     $ cd listPrinters && npm install

   run the app:
     $ DEBUG=listPrinters:* npm start

We can see there are also a few extra instructions given to us just in case we forget.  So, we better run those as well (the && between the command simply means to run both commands in succession... pretty handy feature!):

bash-4.2$ cd listPrinters && npm install
debug@2.2.0 node_modules/debug
└── ms@0.7.1

serve-favicon@2.3.0 node_modules/serve-favicon
├── fresh@0.3.0
├── parseurl@1.3.1
├── etag@1.7.0
└── ms@0.7.1

cookie-parser@1.3.5 node_modules/cookie-parser
├── cookie-signature@1.0.6
└── cookie@0.1.3

morgan@1.6.1 node_modules/morgan
├── on-headers@1.0.1
├── basic-auth@1.0.3
├── depd@1.0.1
└── on-finished@2.3.0 (ee-first@1.1.1)

express@4.13.4 node_modules/express
├── escape-html@1.0.3
├── array-flatten@1.1.1
├── utils-merge@1.0.0
├── cookie-signature@1.0.6
├── merge-descriptors@1.0.1
├── fresh@0.3.0
├── methods@1.1.2
├── range-parser@1.0.3
├── vary@1.0.1
├── path-to-regexp@0.1.7
├── parseurl@1.3.1
├── cookie@0.1.5
├── content-type@1.0.1
├── etag@1.7.0
├── content-disposition@0.5.1
├── serve-static@1.10.2
├── depd@1.1.0
├── on-finished@2.3.0 (ee-first@1.1.1)
├── finalhandler@0.4.1 (unpipe@1.0.0)
├── qs@4.0.0
├── proxy-addr@1.0.10 (forwarded@0.1.0, ipaddr.js@1.0.5)
├── send@0.13.1 (destroy@1.0.4, statuses@1.2.1, ms@0.7.1, mime@1.3.4, http-errors@1.3.1)
├── type-is@1.6.12 (media-typer@0.3.0, mime-types@2.1.10)
└── accepts@1.2.13 (negotiator@0.5.3, mime-types@2.1.10)

body-parser@1.13.3 node_modules/body-parser
├── content-type@1.0.1
├── bytes@2.1.0
├── depd@1.0.1
├── on-finished@2.3.0 (ee-first@1.1.1)
├── http-errors@1.3.1 (inherits@2.0.1, statuses@1.2.1)
├── qs@4.0.0
├── type-is@1.6.12 (media-typer@0.3.0, mime-types@2.1.10)
├── iconv-lite@0.4.11
└── raw-body@2.1.6 (unpipe@1.0.0, bytes@2.3.0, iconv-lite@0.4.13)

jade@1.11.0 node_modules/jade
├── character-parser@1.2.1
├── void-elements@2.0.1
├── commander@2.6.0
├── mkdirp@0.5.1 (minimist@0.0.8)
├── jstransformer@0.0.2 (is-promise@2.1.0, promise@6.1.0)
├── constantinople@3.0.2 (acorn@2.7.0)
├── with@4.0.3 (acorn@1.2.2, acorn-globals@1.0.9)
├── clean-css@3.4.10 (commander@2.8.1, source-map@0.4.4)
├── transformers@2.1.0 (promise@2.0.0, css@1.0.8, uglify-js@2.2.5)
└── uglify-js@2.6.2 (uglify-to-browserify@1.0.2, async@0.2.10, source-map@0.5.3, yargs@3.10.0)

At this point I really have no idea what all this means.  But, let's move on anyhow.

What IP Address and Port Will My New App Run On?

With Apache most of us are used to specifying at least the port in the configuration file.  Because the IBM i allows you to create multiple IP addresses on one NIC, I feel it's always best to supply the IP address and the port to use.

With node, though, it seems they aren't set up for this type of environment.  So, I am going to do some modifications and set the IP address and port myself.

By default this new server will run on the IP address and Port specified by the environment variables process.env.IP and prcess.env.PORT OR localhost and port 3000 if these aren't set.  Because I have an IP address set up specifically for my node testing I want to make sure to use that one.  But how is this done?  The app.js file that is created doesn't show anything about what port and or IP will be used.

So, I did some searching and I found that the file ../<appname>/bin/www (no extension) is what is used to set the port.  Here is the code in question:

/**
 * Get port from environment and store in Express.
 */

var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);

/**
 * Create HTTP server.
 */

var server = http.createServer(app);

/**
 * Listen on provided port, on all network interfaces.
 */

server.listen(port);

What this is saying is, use the process.env.PORT environment variable.  If that isn't available, then use port 3000.  It doesn't even mention what IP address to use (again, because most PCs or servers don't have the neat feature the IBM i does of being able to create multiple IP addresses/interfaces).

So, lets modify this to set the IP address and the port we want to use:

/**
 * Get port from environment and store in Express.
 */

//var port = normalizePort(process.env.PORT || '3000');
//app.set('port', port);

/**
 * Create HTTP server.
 */

var server = http.createServer(app);

/**
 * Listen on provided port, on all network interfaces.
 */

/**
 * Create HTTP server.
 */

var server = http.createServer(app);

/**
 * Listen on provided port, on all network interfaces.
 */

server.listen(8080, '192.168.1.39');

So, all we did was comment out a couple lines and then on the server.listen() function code in the port and IP address we specifically wanted to use.

If we then visit http://192.168.1.39:8080/ we will see the default Welcome message from express.  Next, we'll look at trying to update this app so that we can list the printers available to us via Google Cloud Print.


Last edited 03/29/2016 at 09:50:46


Reply




Copyright 1983-2017 BVSTools
GreenBoard(v3) Powered by the eRPG SDK, MAILTOOL Plus!, GreenTools for Google Apps, jQuery, jQuery UI, BlockUI, CKEditor and running on the IBM i (AKA AS/400, iSeries, System i).