Develop More Flexible Node.js Web and Mobile Apps with Express

Although Node.js offers Web developers a number of benefits, when it comes time to handle requests for different URLs ("routes"), serve static files, or use templates to dynamically create the response, you either need to write the code yourself, or you can avoid a lot of coding by using a Web framework. Of all the Node frameworks in use today, Express is the most popular. It's also the underlying library for a number of other frameworks.

If you haven't gotten your hands dirty with Express yet, you're in for a treat, because in today's article, we're going to learn how Express came about, what's good about it, and finally, how to create a basic app in just minutes.

Some Background

Express was initially released in November 2010 by TJ Holowaychuk. In June 2014, rights to manage the project were acquired by StrongLoop, an American company that was itself acquired by IBM in September 2015. In January 2016, IBM announced that it would place Express under the stewardship of the Node.js Foundation incubator, which should help ensure its continued viability. Moreover, incubation status means that the project will receive mentoring and open source governance from the foundation. So far, so good! Express is going like gang busters and is currently on version 4.16.3.

Installing Express

It should go without saying that, being a Node.js framework, Express can be installed using NPM. You should install it globally so that it can be used to create a Web application using node terminal:

Assuming you've already installed Node.js, create a directory to hold your application, and make that your working directory:

$ mkdir myapp
$ cd myapp

Use the npm init command to create a package.json file for your application:

$ npm init

This command prompts you for a number of things, such as the name and version of your application. For now, you can simply press RETURN to accept the defaults for most of them, except for the application entry point:

entry point: (index.js)

Enter "app.js", or whatever you want the name of the main file to be. If you don't mind calling it "index.js", press RETURN to accept the suggested default file name. Here's a listing of the whole process:

PS I:\My Documents\Express\my-express-app> npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible
defaults.
See `npm help json` for definitive documentation on these fields
and exactly what they do.
Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.
Press ^C at any time to quit.
package name: (my-express-app)
version: (1.0.0)
description:
entry point: (index.js) app.js
test command:
git repository:
keywords:
author:
license: (ISC)
About to write to I:\My Documents\Express\my-express-app
\package.json:
{
"name": "my-express-app",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
Is this ok? (yes)
PS I:\My Documents\Express\my-express-app>

Now, install Express in the myapp directory and save it in the dependencies list. For example:

The app.js Code

The app.js script starts a server and listens on port 8081 for connections. The app responds to GET requests to the root URL (/) with "Hello World". For every other URL, it will automatically respond with a 404 Not Found.

Conclusion

In today's article, we learned how Express came about, what's good about it, and how to create a basic app. In the next installment, we'll look at how Express handles different request types and routes as well as how to use the Express generator, which creates the scaffolding for a full app with numerous JavaScript files, Jade templates, and sub-directories.

IT Solutions Builder
TOP IT RESOURCES TO MOVE YOUR BUSINESS FORWARD

Which topic are you interested in?

Mobile

Security

Networks/IoT

Cloud

Data Storage

Applications

Development

IT Management

Other

What is your company size?

What is your job title?

What is your job function?

Searching our resource database to find your matches...

Please enable Javascript in your browser, before you post the comment! Now Javascript is disabled.

Advertiser Disclosure:
Some of the products that appear on this site are from companies from which QuinStreet receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. QuinStreet does not include all companies or all types of products available in the marketplace.

Thanks for your registration, follow us on our social networks to keep up-to-date