var musicModule = (function () {
// Let's make sure no one can directly access our songList
var songList = ['California Girls', 'California Dreaming', 'Hotel California'];
// We'll expose all these functions to the user
function play () {
console.log('Im playing the next song!');
}const express = require('express');
const app = express();
const albumsRouter = require('./routers/albums');
//...
app.use('/albums', albumsRouter); // Forwards any requests to the /albums URI to our albums Router
//...const albums = require('express').Router();
//...
// Our app.use in the last file forwards a request to our albums router.
// So this route actually handles `/albums` because it's the root route when a request to /albums is forwarded to our router.
albums.get('/', function(req, res, next) {
// res.send() our response here
});const albums = require('express').Router();
const tracks = require('./tracks').Router();
//...
// Our root route to /albums
albums.get('/', function(req, res, next) {
// res.send() our response here
});const tracks = require('express').Router();
//...
// The root router for requests to our tracks path
track.get('/', function(req, res, next) {
let albumId = req.params.albumId; // Our problem line
// retrieve album's track data and render track list pageconst albums = require('express').Router();
const tracks = require('./tracks').Router();
//...
albums.get('/', function(req, res, next) {
// send our response here
});const tracks = require('express').Router();
//...
// The root router for requests to our tracks path
track.get('/', function(req, res, next) {
let albumId = req.albumId; // Refactored to access our albumId property
// retrieve album's track data and render track list pageExpress.js is a Javascript library that handles web routing and HTTP requests for web applications.
Express builds on the native HTTP library in Node.js to allow for a simple, object-oriented approach to route your web application.
The creators of Express.js describe it as a 'minimalist framework', meaning that Express.js handles a few core tasks well, but does not include many nice-to-have features. Instead, you can enhance your Express.js application with middleware downloaded from npm or that you build yourself.
This series has four parts.
First, we'll look at HTTP routing, the problem that Express.js tries to solve.
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.
Now that our app is set up and configured, let's see how Express.js simplifies our routing.
Here's what our app currently looks like.
const express = require('express'); // makes Express available in your app.
const app = express(); // Creates an instance of Express, which allows us to begin routing.