起一个web服务器server.jsvar http = require(‘http’); // 匿名回调函数// http.createServer(function (req, res) { var server = http.createServer(function (req, res) { // 返回请求头的状态码是200,成功 res.writeHead(200, {‘Content-Type’: ’text/plain’}); res.end(‘Hello NodeJs\n’);})// .listen(1337, ‘127.0.0.1’);server.listen(1337, ‘127.0.0.1’);console.log(‘Server running at http://127.0.0.1:1337/’ );浏览器进入 http://127.0.0.1:1337/浏览器特殊的全局变量有:windowdocumentnode环境的全局变量有:process模块与包管理工具Commonjs是一套规范npm install模块的分类及引入方式核心模块: http fs path …文件模块: var util = require(’./util.js’)第三方模块: var promise = require(‘bluebird’)核心模块会在node启动时预先加载模块的流程创建模块: teacher.js导出模块: exports.add = function(){}加载模块: var teacher = require(’./teacher.js’)使用模块: teacher.add(‘Scott’)url方法网址是具体的符号,说明了要用哪种协议访问哪种资源uri字符串格式规范,是概念上的定义url是uri的子集,uri不一定是urlLegacy URL APIurl.format(urlObject)url.parse(urlString[, parseQueryString[, slashesDenoteHost]])url.resolve(from, to)链接:https://nodejs.org/dist/lates…url.format(urlObject)将一个url对象格式化为一个字符串url.parse(urlString[, parseQueryString[, slashesDenoteHost]])解析一个url地址,解析为一个对象url.parse(‘http://imoc.com:8080/course/list?from=scott&course=node#floor1',true)第二个参数是否为true决定了使用QueryString模块还是url自身,true会使query解析成一个对象格式url.resolve(from, to)接收2个参数来解析,能把2个参数拼接成浏览器能够识别的格式url.parse()❤️很重要的方法>node> url{ Url: [Function: Url], parse: [Function: urlParse], resolve: [Function: urlResolve], resolveObject: [Function: urlResolveObject], format: [Function: urlFormat], URL: [Function: URL], URLSearchParams: [Function: URLSearchParams], domainToASCII: [Function: domainToASCII], domainToUnicode: [Function: domainToUnicode] }> url.parse(‘http://www.expressjs.com.cn/')Url { protocol: ‘http:’, //protocol是底层制定的协议 slashes: true, //slashes是否有协议的双斜线 auth: null, host: ‘imoc.com’, //host是http服务器的ip地址或者叫域名 port: null, //端口,默认是80端口 hostname: ‘imoc.com’, //主机名 hash: null, //哈希值,对应所有的锚,即页面上的某一个锚点,url中有加#之后部分的内容,指向了页面滚动位置的某个锚点 search: null, //查询字符串参数 query: null, //发送给http服务器的数据,通常称被等号=间隔开的键值为参数串 pathname: ‘/course/list’, //访问资源路径名 path: ‘/course/list’, //路径 href: ‘http://imoc.com/course/list' } //未被解析的完整超链接,协议和主机名都会被小写化,如果是null,说明对应值在对应地址中没有体现> url.parse(‘http://imoc.com:8080/course/list?from=scott&course=node#floor1')Url { protocol: ‘http:’, slashes: true, auth: null, host: ‘imoc.com:8080’, port: ‘8080’, hostname: ‘imoc.com’, hash: ‘#floor1’, //哈希值,对应所有的锚,即页面上的某一个锚点,url中有加#之后部分的内容,指向了页面滚动位置的某个锚点 search: ‘?from=scott&course=node’, //查询字符串参数 query: ‘from=scott&course=node’, //发送给http服务器的数据,通常称被等号=间隔开的键值为参数串 pathname: ‘/course/list’, path: ‘/course/list?from=scott&course=node’, //路径 href: ‘http://imoc.com:8080/course/list?from=scott&course=node#floor1' } //未被解析的完整超链接> url.parse(‘http://imoc.com:8080/course/list?from=scott&course=node#floor1',false)Url { protocol: ‘http:’, slashes: true, auth: null, host: ‘imoc.com:8080’, port: ‘8080’, hostname: ‘imoc.com’, hash: ‘#floor1’, search: ‘?from=scott&course=node’, query: ‘from=scott&course=node’, pathname: ‘/course/list’, path: ‘/course/list?from=scott&course=node’, href: ‘http://imoc.com:8080/course/list?from=scott&course=node#floor1' }> url.parse(‘http://imoc.com:8080/course/list?from=scott&course=node#floor1',true)Url { protocol: ‘http:’, slashes: true, auth: null, host: ‘imoc.com:8080’, port: ‘8080’, hostname: ‘imoc.com’, hash: ‘#floor1’, search: ‘?from=scott&course=node’, query: { from: ‘scott’, course: ’node’ }, //使用QueryString模块会使query解析成一个对象格式 pathname: ‘/course/list’, path: ‘/course/list?from=scott&course=node’, href: ‘http://imoc.com:8080/course/list?from=scott&course=node#floor1' }> url.parse(’//imoc.com/course/list’, true)Url { protocol: null, //protocol协议为null slashes: null, auth: null, host: null, // port: null, hostname: null, // hash: null, search: ‘’, query: {}, pathname: ‘//imoc.com/course/list’, // path: ‘//imoc.com/course/list’, // href: ‘//imoc.com/course/list’ }> url.parse(’//imoc.com/course/list’, true, true)Url { protocol: null, //protocol协议为null slashes: true, auth: null, host: ‘imoc.com’, //有域名 port: null, hostname: ‘imoc.com’, //有主机名 hash: null, search: ‘’, query: {}, pathname: ‘/course/list’, //路径名字分离 path: ‘/course/list’, //路径分离 href: ‘//imoc.com/course/list’ }url.format()解析一个url对象,看能不能生成一个url地址> url.format({… protocol: ‘http:’,… slashes: true,… auth: null,… host: ‘imoc.com:8080’,… port: ‘8080’,… hostname: ‘imoc.com’,… hash: ‘#floor1’,… search: ‘?from=scott&course=node’,… query: ‘from=scott&course=node’,… pathname: ‘/course/list’,… path: ‘/course/list?from=scott&course=node’,… href: ‘http://imoc.com:8080/course/list?from=scott&course=node#floor1' })‘http://imoc.com:8080/course/list?from=scott&course=node#floor1'>url.resolve()拼接参数> url.resolve(‘http://imoc.com/', ‘/course/list’)‘http://imoc.com/course/list'>QueryString参数处理小利器依赖于路径结合参数告知服务器链接:https://nodejs.org/dist/lates…Query Stringquerystring.escape(str)querystring.parse(str[, sep[, eq[, options]]])querystring.stringify(obj[, sep[, eq[, options]]])querystring.unescape(str)querystring.stringify({})序列化第一个参数,把参数的对象序列化为参数的字符串> querystring.stringify({name: ‘scott’, course: [‘jade’, ’node’], from: ‘’})’name=scott&course=jade&course=node&from=’ //序列化后的字符串第二个参数为连接符,默认为 与&第三个参数为键值对的连接符,默认为 等号=都能修改> querystring.stringify({name: ‘scott’, course: [‘jade’, ’node’], from: ‘’}, ‘,’, ‘:’)’name:scott,course:jade,course:node,from:’ //序列化后的字符串querystring.parse()反序列化,如果第二第三个参数有被变更默认值,比如通过逗号分隔,需要在第二个参数中传入逗号,,键值对的等号被替换为冒号,需要在第三个参数中传入冒号:> querystring.parse(’name=scott&course=jade&course=node&from=’){ name: ‘scott’, course: [ ‘jade’, ’node’ ], from: ’’ }> querystring.parse(’name=scott&course=jade&course=node&from=’){ name: ‘scott’, course: [ ‘jade’, ’node’ ], from: ’’ }> querystring.parse(’name:scott,course:jade,course:node,from:’, ‘,’, ‘:’){ name: ‘scott’, course: [ ‘jade’, ’node’ ], from: ’’ }第四个参数是options,默认数量是1000个,设为0表示不限制querystring.escape()转义> querystring.escape(’<哈哈>’)’%3C%E5%93%88%E5%93%88%3E’querystring.unescape()反转义> querystring.unescape(’%3C%E5%93%88%E5%93%88%3E’)’<哈哈>’