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.

<h2>Quote by <%= name %></h2>

<blockquote>
<%= quote %>
</blockquote>

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 format 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 request module to make a simple web request and log the response to the console. You can use this example 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 here
app.get('/', function(req, res){
request(searchURL).pipe(res);
}).listen(8080);

[Node.js] Level 5. Express的更多相关文章

  1. node.js 下依赖Express 实现post 4种方式提交参数

    上面这个图好有意思啊,哈哈, v8威武啊.... 在2014年的最后一天和大家分享关于node.js 如何提交4种格式的post数据. 上上一篇说到了关于http协议里定义的4种常见数据的post方法 ...

  2. Node.js的框架-express

    Node.js的框架 express 是第三方的 express const express=require('express'); const app=express(); const PORT=3 ...

  3. Node.js 实战 & 最佳 Express 项目架构

    Node.js 实战 & 最佳 Express 项目架构 Express Koa refs https://github.com/xgqfrms/learn-node.js-by-practi ...

  4. node.js入门及express.js框架

    node.js介绍 javascript原本只是用来处理前端,Node使得javascript编写服务端程序成为可能.于是前端开发者也可以借此轻松进入后端开发领域.Node是基于Google的V8引擎 ...

  5. node.js 基础学习 express安装使用

    安装好nodeJs,我们需要使用命令行中安装express. 我这里默认将Node.js安装在C:\Program Files\nodCejs\盘中. 在保持联网的状态下,依次输入如下命令. npm ...

  6. 建立一个node.js服务器(使用express搭建第一个Web环境)

    一.官网下载node.js 下载地址:https://nodejs.org/en/download/ 根据向导,下一步安装就可以了! 二.使用express搭建Web环境 express是一个开源的n ...

  7. Node.js 入门:Express + Mongoose 基础使用

    前言 Express 是基于 Node.js 平台的 web 应用开发框架,在学习了 Node.js 的基础知识后,可以使用 Express 框架来搭建一个 web 应用,实现对数据库的增删查改. 数 ...

  8. Node.js原生及Express方法实现注册登录原理

    由于本文只是实现其原理,所以没有使用数据库,只是在js里面模拟数据库,当然是种中还是需要用数据库的. 1.node.js原生方法 ①html页面,非常简单,没有一丝美化~我们叫它user.html & ...

  9. node.js上除了Express还有哪些好用的web开发框架

    老司机都有体会, 开发本身没有多难, 最纠结其实是最初的技术和框架选型, 本没有绝对的好坏之分, 可一旦选择了不适合于自己业务场景的框架, 将来木已成舟后开发和维护成本都很高, 等发现不合适的时候更换 ...

随机推荐

  1. 使用 Eigen 3.3.3 进行矩阵运算

    Eigen是一个能够进行线性代数运算的C++开源软件包,包含矩阵和矢量操作,Matlab中对矩阵的大多数操作都可以在Eigen中找到. 最近需要计算厄米特矩阵的逆,基于LLT分解和LDLT分解,自己写 ...

  2. logging记录日志

    日志是一个系统的重要组成部分,用以记录用户操作.系统运行状态和错误信息.日志记录的好坏直接关系到系统出现问题时定位的速度.logging模块Python2.3版本开始成为Python标准库的一部分. ...

  3. CentOS服务器安装Telnet来远程连接服务器

    0.目录 整体架构目录:ASP.NET Core分布式项目实战-目录 一.前言 在连接远程服务器时有很多种连接方式,如SSH.telnet.SFTP等.但是如果大家在docker上面安装gitlab做 ...

  4. Tweet信息搜集工具tinfoleak

    Tweet信息搜集工具tinfoleak   推特是国外用户常用的社交网站.通过分析用户发布的推文以及社交活动,可以获取大量的个人信息.Kali Linux新增一款Tweet信息搜索工具tinfole ...

  5. JavaScript with JSONPath

    <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>JavaScript JSO ...

  6. Ubuntu系统安装网易云音乐、搜狗输入法

    这两个软件都很良心,提供了Ubuntu版本,直接下载安装即可. 网易云音乐: 下载-打开-安装 http://music.163.com/#/download 搜狗拼音输入法 下载-打开-安装 htt ...

  7. [APIO2010]特别行动队 --- 斜率优化DP

    [APIO2010]特别行动队 题面很直白,就不放了. 太套路了,做起来没点感觉了. \(dp(i)=dp(j)+a*(s(i)-s(j))^{2}+b*(s(i)-s(j))+c\) 直接推出一个斜 ...

  8. ajax请求jesery接口无法获取参数的问题解决方案

    jesery是强大的RESTful api框架, 很多人在用它做web项目时会遇到这样一个问题: ajax请求jesery接口无法获取输入参数, 可明明接口已经指明了Consume是applicati ...

  9. hdu 1565 最小割

    黑白染色,源指向白,黑指向汇,容量都是方格中数的大小,相邻的格子白指向黑,容量为oo,然后求一次最小割. 这个割是一个简单割,如果只选择不在割中的点,那么一种割就和一个选数方案一一对应,割的大小就是不 ...

  10. React-如何在jsx中自动补全标签(vscode)

    痛点:  React库最近的增长趋势很明显, 很多朋友都在选择学习, 很多公司也在选择使用React栈. 但在使用React库写代码的时候, 有一个很让人苦恼的问题, 就是标签在jsx语法中不能自动补 ...