Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Building Apps with Node.js and Express

Nick McCurdy
November 05, 2014

Building Apps with Node.js and Express

A walkthrough for building simple server-side web apps with the Express framework for Node.js.

Nick McCurdy

November 05, 2014
Tweet

More Decks by Nick McCurdy

Other Decks in Technology

Transcript

  1. “Node.js® is a platform built on Chrome's JavaScript runtime for

    easily building fast, scalable network applications.” - nodejs.org What is Node.js?
  2. What is Express? • Express is a web framework built

    on top of Node.js. • Inspired by the Sinatra web framework. • Helps you create modular web applications. • Based around the concept of middleware.
  3. Installing Node.js and Express • Want to follow along? Get

    this presentation at sse.se.rit. edu/go/expresstalk. • Install Node.js. Windows and Mac: Use the Installer from nodejs.org/download. Linux: Look for “node” in your package manager or build it from source. • Install Express. Run npm install express in the directory where you want to put your app.
  4. Hello World 1. Create app.js. var express = require('express'); //

    load the Express module var app = express(); // create a new Express app // Respond to requests for / with “Hello World!”. app.get('/', function (request, response) { response.send('Hello World!'); }); // Start a server with the Express app on port 3,000. var server = app.listen(3000, function () { console.log('Example app listening at http://localhost:3000/'); });
  5. Hello World 2. Run node app.js. 3. Open http://localhost:3000 in

    your web browser. 4. If you want to create a package.json for sharing your app and installing dependencies, run npm init.
  6. Middleware • A function executed by the Express router after

    a request but before the final route. • Middleware can be used for logging, error handling, template rendering, routing, etc.
  7. Using Middleware • Express’s app.use() can be passed your own

    middleware function or a function from a middleware library to add it to all requests. • In your middleware function, you must call next() when you’re done to let other middleware run (unless you want to end the middleware stack). • Order matters!
  8. Time Logging with Middleware var express = require('express'); var app

    = express(); app.use(function (request, response, next) { console.log('Time: ' + new Date()); next(); }); app.get('/', function (request, response) { response.send('Hello World!'); }); app.listen(3000);
  9. Express Generator The Express generator is a useful tool which

    creates a modular skeleton for new Express projects.
  10. Using the Express Generator 1. Install the Express app generator

    a. npm install express-generator -g 2. Use the generator to create a new app a. express --ejs example b. cd example c. npm install 3. Edit your app however you’d like and run it with npm start.
  11. Useful npm Commands • npm install Installs all your dependencies.

    • npm install --save <package> Installs a specific package and saves it to your dependencies. • npm start Runs your app. • npm test Runs your tests (if you have a test script in package.json).
  12. FAQ • What about models? Express doesn’t have models. You

    can set up model objects yourself or use a library. • Is it production ready? Yes, it is used in production by many large organizations. • What CSS preprocessors and templating engines are supported? The Express app generator supports several including ejs, jade, and less, but you can use pretty much anything that conforms to Express’s templating APIs.