5.2 Express Routes
Let's create an express route that accepts GET requests on '/tweets' and responds by sending back a static HTML file.
Task 1:
Create a GET route for '/tweets' and give it the proper callback. The callback function should accept two arguments: the request and the response.
Task 2:
Send back the file
Task 3:
Finally, have the express app listen on port 8080.
```js var express = require('express'); var app = express(); app.get('/tweets', function(request, response){ response.sendFile( __dirname + '/tweets.html'); }).listen(8080); ```
5.3 Route Params
Let's create a route that accepts dynamic arguments in the URL path and responds with the quote from the proper author.
Task 1:
Start by creating a GET route for '/quotes' that takes a
Task 2:
Now, use the
```js 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(req, response){ response.end(quotes[req.params.name]); }); app.listen(8080); ```
5.4 Rendering
Instead of just writing out the quote to the response, let's try using an EJS template to render the response.
Task 1:
First, render the
Task 2:
Next, make the
Task 3:
Inside
app.js ```js 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(req, res) { var quote = quotes[req.params.name]; res.render('quote.ejs', { name: req.params.name, quote: quote }); }); app.listen(8080); var express = require('express'); ``` views/quote.ejs ```js Quote by <%= name %> <blockquote> <%= quote %> </blockquote> ```
5.5 URL Building
Let's create a page which calls the Twitter search API and displays the last few results for Code School. The first step is to construct the proper URL, which is all you need to do in this challenge.
Complete the URL options which will be sent into the the
Task 1:
Add the
Task 2:
Add the
Task 3:
Add the
Task 4:
Add an attribute which takes an object of query parameters, in this case we only need
```js var url = require('url'); options = { // add URL options here protocol: 'http:', host : 'search.twitter.com', pathname : '/search.json', query: {q:'codeschool'} }; var searchURL = url.format(options); console.log(searchURL); ```
5.6 Doing the Request
Next, we'll use the request module to make a simple web request and log the response to the console. You can use this example in the README.
Task 1:
To start, require the
Task 2:
Next, issue a request to
Task 3:
Finally, log the response body to the console using
5.7 Express Server
Now, let's create an Express server which queries out for the search term and then returns the JSON.
Task 1:
Require the
Task 2:
Create the Express server and name it
Task 3:
Create a route for GET requests to
Task 4:
In our new route, issue a request to
Task 5:
Finally, tell
```js var url = require('url'); var request = require('request'); var express = require('express'); var options = { protocol: "http:", host: "search.twitter.com", pathname: '/search.json', query: { q: "codeschool" } }; var searchURL = url.format(options); var app = express(); app.get('/', function(req, res){ request(searchURL).pipe(res); }).listen(8080); ```
沒有留言:
張貼留言