0

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);
5
  • Sorry, you're going to need to give me a bit more than that. What exactly is the issue, and what do you mean by you're blocked there? Commented Nov 7, 2014 at 9:43
  • the issue is that apparently i dont send the quoute in my response.send function Commented Nov 7, 2014 at 9:44
  • Well, that makes sense. You're just sending the value of the parameter :name in the response... Commented Nov 7, 2014 at 9:45
  • 1
    Your problem has nothing to do with Node, it's basic JavaScript. quotes[request.params.name]. Commented Nov 7, 2014 at 9:47
  • Yes Ben i've tried that too quotes[request.params.name] and quotes.request.params.name even quotes["request.params.name"] but it doesn't work Commented Nov 7, 2014 at 9:48

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

9 Comments

Doesn't seems to work, perhaps problem with browser i gonna try to restart exercise, who knows ;]
This is literally the code I put into my app.js and ran it, and it works fine for me.
seems it was browser, just last question, now they ask me to close the response and response.send(quotes[request.params.name]).end(); doesn't seems to please them -_-
Try doing response.end() on a separate line.
OK I found out (not really used a hint but who cares) it was response.end(quote)... Thanks southrop !
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.