~) is a shortcut to the home directory. Note the two ampersands (
&&) are a way to combine two commands on a single line.
npm initto create what's called the
package.jsonfile which will hold information about the application like the author, a description, and dependencies. You will be prompted for many values, as shown below. Provide input or hit the
Enterkey to accept the default.
packages.jsonand can view it using the
catcommand, as shown below.
npmto add express as a dependency in
expressmodule you should see a log, as shown above. When
expressit looks at its
package.jsonfile to learn what other dependencies need to be downloaded and also obtains them. This is a recursive process until the end of the dependency list is reached. All the downloaded modules are stored in the
node_modules/directory of your current directory.
app1and paste the following content into it so we can see Express in action.
expressmodule. Notice there isn't a relative path specified. You can omit the path because the express module is located in the
node_modules/directory, which is part of the search path when loading modules.
'/') defined so when we bring up the root of the website it will display "Hello World!".
||) is the or operator which will first check whether
process.env.PORThas a value. If not, it will occupy it with the value to the right of the vertical pipes (i.e.
process.envobject gives us access to environment variables from the current process (aka IBM i job). What this means is an overriding port can be passed when starting the application, as shown below. Go ahead and try running your app on the various ports dedicated to your profile.
PORT=60263is specified before the node
index.jsportion. This is valid syntax and is basically setting the
PORTenvironment variable for only the duration of this particular call to the node binary. If you wanted to have the
PORTenvironment variable to be set at the process (aka IBM i job) level then you'd use