• No se han encontrado resultados

Capítulo 2 Delimitación del Problema

2.1 Formulación de la pregunta problema

There are loads of third-party modules for Express. Some are made specifically for Express and are compatible with its routing and middleware features. Others aren’t made for Express specifically and work well in Node.js, so they also work well with Express.

In this book, we’ll pick a number of third-party integrations and show examples. But because Express is unopinionated, none of the contents of this book are the only options. If I cover Third-Party Tool X in this book, but you prefer Third-Party Tool Y, you can swap them out.

Express has some small features for rendering HTML. If you’ve ever used vanilla

PHP or a templating language like ERB, Jinja2, HAML, or Razor, you’ve dealt with ren- dering HTML on the server. Express doesn’t come with any templating languages built in, but it plays nicely with almost every Node.js-based templating engine, as you’ll see. Some popular templating languages come with Express support, but others need a simple helper library. In this book, we’ll look at two options: EJS (which looks a lot like

HTML) and Pug (which tries to fix HTML with a radical new syntax).

Express doesn’t have any notion of a database. You can persist your application’s data however you choose: in files, in a relational SQL database, or in another kind of storage mechanism. In this book, we’ll cover the popular MongoDB database for data storage. As we talked about earlier, you should never feel boxed in with Express. If you want to use another data store, Express will let you.

Users often want their applications to be secure. There are a number of helpful libraries and modules (some for raw Node.js and some for Express) that can tighten the belt of your Express applications. We’ll explore all of this in chapter 10 (which is one of my favorite chapters, personally). We’ll also talk about testing your Express code to make sure that the code powering your apps is robust.

An important thing to note: there’s no such thing as an Express module—only a Node.js module. A Node.js module can be compatible with Express and work well with its API, but they’re all just JavaScript served from the npm registry, and you install them the same way. Just like in other environments, some modules integrate with other modules, where others can sit alongside. At the end of the day, Express is just a Node.js module like any other.

Getting help when you need it

I really hope this book is helpful and chock-full of knowledge, but there’s only so much wisdom one author can jam into a book. At some point, you’re going to need to spread your wings and find answers. Let me do my best to guide you:

For API documentation and simple guides, the official http://expressjs.com/ site is the place to go. You can also find example applications all throughout the Express repository, at https://github.com/strongloop/express/tree/master/examples. I found these examples helpful when trying to find the right way to do things. There are loads of examples in there; check them out!

16 CHAPTER 1 What is Express?

1.6

The obligatory Hello World

Every introduction to a new code thing needs a Hello World, right?

Let’s look at one of the simplest Express applications you can build: the “Hello World.” We’ll delve into this in much greater detail throughout the book, so don’t worry if not all of this makes sense right now. Here’s Hello World in Express.

var express = require("express");

var app = express();

app.get("/", function(request, response) { response.send("Hello, world!"); });

app.listen(3000, function() { console.log("Express app started on port 3000."); });

Once again, if not all of this makes sense to you, don’t worry. But you might be able to see that you’re creating an Express application, defining a route that responds with “Hello, world!”, and starting your app on port 3000. There are a few steps you’ll need to do to run this—all of that will become clear in the next couple of chapters.

You’ll learn all of Express’s secrets soon.

1.7

Summary

■ Node.js is a powerful tool for writing web applications, but it can be cumber- some to do so. Express was made to smooth out that process.

■ Express is a minimal, unopinionated framework that’s flexible.

(continued)

For Node.js modules, you’ll be using Node.js’s built-in npm tool and installing things from the registry at https://www.npmjs.com/. If you need help finding good modules, I’d give Substack’s “finding modules” a read at http://substack.net/finding_modules. It’s a great summary of how to find quality Node.js packages.

Express used to be built on another package called Connect, and it’s still largely com- patible with Connect-made modules. If you can’t find a module for Express, you might have luck searching for Connect. This also applies if you’re searching for answers to questions. And as always, use your favorite search engine.

Listing 1.1 Hello World in Express

Requires Express and puts it in a variable

Calls express() and puts new Express application inside the app variable Sends “Hello,

world!”

Starts the Express server on port 3000 and logs that it has started

17 Summary

Express has a few key features:

– Middleware which is a way to break your app into smaller bits of behavior. Generally, middleware is called one by one, in a sequence.

– Routing similarly breaks your app up into smaller functions that are exe- cuted when the user visits a particular resource; for example, showing the homepage when the user requests the homepage URL.

– Routers can further break up large applications into smaller, composable subapplications.

Most of your Express code involves writing request handler functions, and

18

The basics of Node.js

In chapter 1, we described Node.js, explaining that it’s JavaScript, asynchronous, and has a rich set of third-party modules. If you’re like me, you didn’t totally under- stand these things when you first started with Node. This chapter aims to give the intro to Node that I wish I had: short and sweet. (From here forward, I’ll refer to Node.js simply as Node.)

NOTE I’m assuming that you know a fair bit of JavaScript and that you don’t want an extremely thorough knowledge of Node from this chapter. I’m also going to assume that you have a working understanding of how to use the command line. If this whirlwind introduction to Node is a little too

This chapter covers

■ Installing Node.js and using its module system

■ Using package.json to describe your project’s metadata

■ Using npm to install packages with npm install

■ Doing two things at once with Node

■ Using Node’s built-in http module to build a simple web server

19