博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Node.js] Level 5. Express
阅读量:5341 次
发布时间:2019-06-15

本文共 4518 字,大约阅读时间需要 15 分钟。

Express Routes

Let's create an express route that accepts GET requests on'/tweets' and responds by sending back a static HTML file.

Create a GET route for '/tweets' and give it the proper callback. The callback function should accept two arguments: the request and the response.

Send back the file tweets.html, which lives under the project's root path. Remember to use __dirname to locate tweets.html.

Finally, have the express app listen on port 8080.

var express = require('express');var app = express();app.get('/tweets', function(request, response){    response.sendFile( __dirname + '/tweets.html');}).listen(8080);

 

Route Params 250 PTS

Let's create a route that accepts dynamic arguments in the URL path and and responds with the quote from the proper author.

Start by creating a GET route for '/quotes' that takes a name parameter as part of the URL path.

Now, use the name parameter from the URL to retrieve a quote from the quotes object and write it out to the response. Note: No piping here, just write the quote string to the response like you did in previous levels (and then close the response).

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);

 

 Rendering

Instead of just writing out the quote to the response, let's try using an EJS template to render the response.

First, render the quote.ejs template to the response.

Next, make the name and the quote data available to the template.

res.locals = {name: req.params.name, quote: quote};

Inside quote.ejs, add the code needed to render the data you passed to the template.

Quote by <%= name %>

<%= quote %>

 

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 urlmodule's  method. The URL you'll want to construct is the following: http://search.twitter.com/search.json?q=codeschool

Add the protocol attribute to options.

Add the host attribute to options.

Add the pathname attribute to options.

Add an attribute which takes an object of query parameters, in this case we only need q to search Twitter.

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);

 

Doing the Request

Next, we'll use the  module to make a simple web request and log the response to the console. You can use  in the README.

To start, require the request module and assign the return function to a variable.

Next, issue a request to searchURL. Remember, the callback function for the request function takes three arguments: errorresponse and body.

Finally, log the response body to the console using console.log().

var url = require('url');var options = {  protocol: "http:",  host: "search.twitter.com",  pathname: '/search.json',  query: { q: "codeschool"}};var searchURL = url.format(options);var request = require('request');request(searchURL, function(error, response , body){    console.log(body);});

 

Express Server

Now, let's create an Express server which queries out for the search term and then returns the JSON.

Require the express module.

Create the Express server and name it app.

Create a route for GET requests to / (root path). Remember, the callback function takes two arguments: a request req and a response res.

In our new route, issue a request to searchURL and pipe the results into the response.

Finally, tell app to listen on port 8080.

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(); // Create server hereapp.get('/', function(req, res){    request(searchURL).pipe(res);}).listen(8080);

 

转载于:https://www.cnblogs.com/Answer1215/p/4091042.html

你可能感兴趣的文章
Redis 详解 (三) redis的五大数据类型详细用法
查看>>
MBProgressHUD 显示后,为何不能点击屏幕其他地方
查看>>
警惕!面部识别技术正在开始构成重大隐私威胁
查看>>
关于Git的一些操作记录
查看>>
Google的开源C++单元测试框架Google Test
查看>>
Qt Installer Framework 使用说明(三)
查看>>
Memory Map
查看>>
java 集合之 Map
查看>>
Java线程的5种状态及切换
查看>>
JS基础(下)
查看>>
IntelliJ IDEA default settings 全局默认设置
查看>>
review14
查看>>
这个日本大叔变态地扔了98%的家当后,瞬间从屌丝逆袭成人生赢家!
查看>>
标准io(带缓存IO)
查看>>
typedef和define
查看>>
angular在1.5.5以上文件上传进度监控
查看>>
java中的守护线程
查看>>
上限值可以这么写
查看>>
mysql安装的一些问题
查看>>
varnish简单应用
查看>>