AlexDom
AlexDom

Reputation: 701

Express route with dynamic arguments in the URL

first of all i am a very beginner of Node so my question gonna be surely basic. So, i learn Node with a website and i am blocked on a one Express exercice. I need to create a route that accepts dynamic arguments in the URL path and responds with quote (object see after in code) from the proper author. The js is like follows :

var express = require('express');
var app = express();

var quotes = {
  'einstein': 'Life is like riding a bicycle. To keep your balance you must keep moving',
  'berners-lee': 'The Web does not just connect machines, it connects people',
  'crockford': 'The good thing about reinventing the wheel is that you can get a round one',
  'hofstadter': 'Which statement seems more true: (1) I have a brain. (2) I am a brain.'
};

app.get('/quotes/:name', function(request, response){
  response.send(request.params.name); /*And i'm blocked here*/
});
app.listen(8080);

Upvotes: 0

Views: 114

Answers (1)

southrop
southrop

Reputation: 909

Try this for the line you're blocked at:

response.send(quotes[request.params.name]);

This is conditional on the :name being right though, so you may want to check it exists in your quotes object with Object.hasOwnProperty() first.

Upvotes: 2

Related Questions