Skip to content

Instantly share code, notes, and snippets.

@zcaceres
Last active May 22, 2017 16:36
Show Gist options
  • Save zcaceres/ca1ca268df300a10197b8dc8e1d3effb to your computer and use it in GitHub Desktop.
Save zcaceres/ca1ca268df300a10197b8dc8e1d3effb to your computer and use it in GitHub Desktop.

Installing Express.js

To install Express.js, you'll first initialize an npm project in a directory of your choice. Then, create a file where you'll build your app. I'm calling mine app.js.

Now, run npm install --save express from your favorite command line interface. This will install Express along with its many dependencies.

You'll also need an up-to-date version of Node.js, since Express leverages Node's HTTP to work its magic.

Once Express.js is installed, we're ready to start building our app.

Require and Instantiate An Express App

First require Express. This makes the Express module available in our app.js file.

Our call to require('express') returns a function that we can invoke to create a new Express application instance.

        // Inside app.js file
        const express = require('express'); // makes Express available in your app.

Now, we can invoke express and make our Express application available under the variable name app.

        const app = express();
        // Creates an instance of express, which allows us to begin routing.

With Express properly imported and our app instantiated, we're ready to start our server!

        app.listen(3000); // Starts up our server on port 3000.

app.listen(port) starts up our server, listening on the port that we pass in.

        module.exports = app;
        // Allows other files to access our Express app.

Finally, let's make our app available to other files by putting our app into module.exports.

Congratulations, you have launched a web server! Now, let's get it to do something.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment