【nodejs】node基础

发布时间:2019-06-15 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了【nodejs】node基础脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

起一个web服务器

server.js

VAR 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 NodeJsn');
})
// .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/

浏览器特殊的全局变量有:
window
document

node环境的全局变量有:
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不一定是url

Legacy URL API
url.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',f
alse)
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 String
querystring.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')
'<哈哈>'

脚本宝典总结

以上是脚本宝典为你收集整理的【nodejs】node基础全部内容,希望文章能够帮你解决【nodejs】node基础所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。