var http=require("http");
var url=require("url");
var pages=[
{id:"1",route:"",output:"Woohoo!"},
{id:"2",route:"about",output:"A simple routing width node example"},
{id:"3",route:"another page",output:function(){
return "Here \'s "+this.route;
}},
]
http.createServer(function(request,response){
var id=url.parse(decodeURI(request.url),true).query.id;
if (id) {
pages.forEach(function(page){
if(page.id===id){
response.writeHead(200,{"Content-Type":"text/html"});
response.end(typeof page.output ==="function" ? page.output() : page.output);
}
})
}
if (!response.finished) {
response.writeHead(404);
response.end("Page Not Found!");
}
}).listen(8080);

与增加的ID属性可以访问我们的目标数据,例如,

本地的:8080?id = 2。

node解析查询字符串的更多相关文章

  1. 《JS高级程序设计》笔记 —— 解析查询字符串

    今天在继续翻阅<JS高级程序设计>的时候,正好翻到location对象这一小节,其中有一部分就是讲的解析查询字符串.看到这个内容立马想到了做去哪儿秋招笔试题的时候有这么一道题. 去哪儿笔试 ...

  2. 逐个访问URL的每个查询字符串参数

    下面介绍一个函数,用于处理location.search的结果,以解析查询字符串,然后返回包含所有参数的一个对象. 比如  www.baidu.com?q=javascript&num=10 ...

  3. 使用Location对象查询字符串参数

    location是BOM中最有用的对象之一: 1.它提供了与当前窗口中加载的文档有关的信息: 2.他还提供了一些导航功能. location对象的属性有: hash, host, hostname, ...

  4. location对象查询字符串参数

    虽然location.search可以返回从问号到URL末尾的所有内容,但却没有办法逐个访问其中的每个查询字符串参数.为此,可以创建下面这样一个函数,用以解析查询字符串,然后返回包含所有参数的一个对象 ...

  5. 解析URL查询字符串参数为对象以及老浏览器的getElementsByClassName

    高程3使用拼接字符串形式解析的查询字符串,网上有各种正则方式解析的,记得太多,临时需要写的时候,自己都搞混乱了.只记一种吧,用正则. function getQueryStringArgs() { v ...

  6. querystring 解析url 查询字符串

    对前端同学来说,经常要碰到一种比较麻烦的情况,那就是url查询字符串的解析问题.说起来也不难,就是比较麻烦. 具体来处理这种情况的时候,相信有一部分同学就是针对具体项目中的需要的字符去正则匹配一下,业 ...

  7. 将查询字符串解析转换为泛型List的名值集合.

    ///<summary> ///将查询字符串解析转换为泛型List的名值集合. ///</summary> ///<param name="queryStrin ...

  8. parse_str() 函数把查询字符串解析到变量中。

    定义和用法 parse_str() 函数把查询字符串解析到变量中. 注释:如果未设置 array 参数,则由该函数设置的变量将覆盖已存在的同名变量. 注释:php.ini 文件中的 magic_quo ...

  9. 黑马旅游网 解析url查询字符串

    function getUrlParam(name) { let reg = new RegExp("(^|&)" + name + "=([^&]*)( ...

随机推荐

  1. 进程和cpu绑定

    #include<stdlib.h> #include<stdio.h> #include<sys/types.h> #include<sys/sysinfo ...

  2. String面试题

    //a b c 分别是怎么存储的, a和b a和c分别有什么区别// c和d的区别是什么 String a= "hello";String b= "hello" ...

  3. advance 模板 怎么生成module

    advance 模板 怎么生成module namespace写什么如果是前台呢就是 frontend\modules\modulename\Module@我叫红领巾 module id有什么用bak ...

  4. 【学习总结】iOS 数据保存几种方式总结

    在iOS开发过程中,不管是做什么应用,都会碰到数据保存的问题.将数据保存到本地,能够让程序的运行更加流畅,不会出现让人厌恶的菊花形状,使得用户体验更好.下面介绍一下数据保存的方式: NSKeyedAr ...

  5. easyui toolbar 可以放在datagrid底下

    html: <div class="easyui-tabs" style="height: 250px;" tools="#t_rank&quo ...

  6. windows android studio 编译Jni动态库

    项目需要,折腾了半天搞定windows android studio环境编译Jni动态库,现记录下来. 准备安装环境: 1. android studio 下载地址是http://www.androi ...

  7. C# testJsonAsXMLNodeAttribute - XML& json & Collections - XmlNode, XmlElement, XmlAttribute,Dictionary,List

    testJsonAsXMLNodeAttribute using Newtonsoft.Json; using System; using System.Collections.Generic; us ...

  8. mysql myisam

    .frm .myd .myi insert delayted show variables like '%delayed%' lock read, write, read local pointer ...

  9. uva 10106

    尝试一下java 的大数类 import java.util.*; import java.io.*; import java.math.BigInteger; public class Main { ...

  10. oracle 用户 多个表空间

    首先,授权给指定用户. 一个用户的默认表空间只能有一个,但是你可以试下用下面的语句为其授权在别的表空间中创建对像: alter user  username quota 0||unlimited on ...