美文网首页
NodeJS学习笔记

NodeJS学习笔记

作者: 未vv | 来源:发表于2018-11-23 10:20 被阅读0次

1.创建http服务

const http = require('http');
const port = 3000;
const hostname = '127.0.0.1';
const server = http.createServer((req,res)=>{
  res.writeHead(200,{"Content-Type":"text/html;charset=utf8"});
  res.write("hello NodeJS!");
  res.end();
})
server.listen(port,hostname,()=>{
  console.log(`Server is running at http://${hostname} : ${port} !`);
})

官网的写法

const http = require('http');
const port = 3000;
const hostname = '127.0.0.1';
const server = http.createServer((req,res)=>{
  res.statusCode = 200;
  res.setHeader('Content-type','text/html');
  res.write("hello NodeJS!");
  res.end('Hello world\n');
})
server.listen(port,hostname,()=>{
  console.log(`Server is running at http://${hostname} : ${port} !`);
})

2.url 模块

const url = require('url');
console.log(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] }

1.url.parse() 解析url

console.log(url.parse('http://www.baidu.com'));
Url {
  protocol: 'http:',
  slashes: true,
  auth: null,
  host: 'www.baidu.com',
  port: null,
  hostname: 'www.baidu.com',
  hash: null,
  search: null,
  query: null,
  pathname: '/',
  path: '/',
  href: 'http://www.baidu.com/' }
console.log(url.parse('http://www.baidu.com/news?name=zhangsan&age=18'))
Url {
  protocol: 'http:',
  slashes: true,
  auth: null,
  host: 'www.baidu.com',
  port: null,
  hostname: 'www.baidu.com',
  hash: null,
  search: '?name=zhangsan&age=18',
  query: 'name=zhangsan&age=18',   //get请求的参数
  pathname: '/news',
  path: '/news?name=zhangsan&age=18',
  href: 'http://www.baidu.com/news?name=zhangsan&age=18' }
console.log(url.parse('http://www.baidu.com/news?name=zhangsan&age=18',true));//query被转为对象
Url {
  protocol: 'http:',
  slashes: true,
  auth: null,
  host: 'www.baidu.com',
  port: null,
  hostname: 'www.baidu.com',
  hash: null,
  search: '?name=zhangsan&age=18',
  query: { name: 'zhangsan', age: '18' },
  pathname: '/news',
  path: '/news?name=zhangsan&age=18',
  href: 'http://www.baidu.com/news?name=zhangsan&age=18' }

2.url.format() ==> url.parse()的逆操作

console.log(url.format({
protocol: 'http:',
  slashes: true,
  auth: null,
  host: 'www.baidu.com',
  port: null,
  hostname: 'www.baidu.com',
  hash: null,
  search: '?name=zhangsan&age=18',
  query: { name: 'zhangsan', age: '18' },
  pathname: '/news',
  path: '/news?name=zhangsan&age=18',
  href: 'http://www.baidu.com/news?name=zhangsan&age=18'
}))
//http://www.baidu.com/news?name=zhangsan&age=18

3.url.resolve() ==> 生成url

console.log(url.resolve('http://www.baidu.com','news'))   //http://www.baidu.com.news
console.log(url.resolve('http://www.baidu.com/one','two'))//http://www.baidu.com/two

3.模块路径解析规则

项目目录


image.png

其中package.json

{
    "name":"foo",
    "main":"foo.js"
}

index.js

const tools = require('tools');
//tools默认在目录下面没有,没有的话nodejs会在node_modules里面找这个模块
console.log(tools);

const bar = require('bar/bar');
console.log(bar);

const nav = require('nav');//当模块的文件名是index.js,加载模块时可以使用模块所在目录的路径代替模块文件路径
console.log(nav);

const foo = require('foo');//想自定义入口模块的文件名和存放位置,就需要在包目录下包含一个package.json文件,并在其中指定入口模块的路径
console.log(foo);

4.常用npm 命令

1.安装模块 npm install ModuleName
2.卸载模块 npm uninstall ModuleName
3.查看当前目录下已安装的Node包 npm list
4.查看模块的版本 npm info ModuleName
5.安装指定版本的模块 npm install ModuleName@
npm install jquery@1.8.2
6.npm install 会从package.json下载模块
7.创建package.json文件 npm init --yes
8.注意:安装模块的时候我们要把这个模块写入到pakage.json这个配置文件中 ,否则其他人在使用我们的项目时会出现问题
npm install ModuleName --save / npm install ModuleName --save-dev 会把我们要安装的模块写入到package.json这个配置文件
其中 --save会把模块写入到pakage.json的 dependencies里
--save-dev会把模块写入到pakage.json的 devDependencies里

9.pakage.json文件中的版本号 符号

"dependencies": {
    "_silly-datetime@0.1.2@silly-datetime": "^0.1.2",
    "silly-datetime": "^0.1.2"
  }

^ 表示第一版本号不变,后面两位取最新的
~ 表示前两位不变,最后一个取最新
* 表示全部取最新

相关文章

  • 2018-08-21nodejs

    Nodejs学习笔记 一、 NodeJs介绍 什么是NodeJS,在应用程开发中起什么作用? Nodejs是一个应...

  • Nodejs学习笔记-Nodejs介绍

    什么是Node.js 编写高性能网络服务器的JavaScript工具包(用js开发服务端程序)单线程、异步、事件驱...

  • nodejs学习笔记

    JavaScript模块编译 在编译过程中,node对获取的JavaScript文件内容进行了头尾包装。正常的Ja...

  • nodejs学习笔记

    模块 名词解释:每一个js文件就是一个模块,而文件路径就是模块名。每个模块(也就是每个js文件)都有requir,...

  • Nodejs学习笔记②

    写在前面 这次做一个小小的登陆&注销登陆功能练习下所学的知识,并扩充些新知识。 目录 新建 login 项目 下载...

  • Nodejs学习笔记①

    写在前面 undefined 目录 检查更新node&npm版本 安装Express 4.x Express 4....

  • nodejs学习笔记

    Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环境。Node.js 使用了一个...

  • Nodejs学习笔记

    Node.js 是单进程单线程应用程序,但是通过事件和回调支持并发,所以性能非常高。nodejs由以下模块组成:引...

  • NodeJS 学习笔记

    NodeJS使用 CommonJS 模块系统。整个项目都是由一个个模块组成的,模块的存在形式是文件,他们一一对应。...

  • Nodejs学习笔记

    以前学习C、OC、Swift、H5的时候都没有留下痕迹,心里甚是遗憾,最近用Nodejs参与了一个web开发,果断...

网友评论

      本文标题:NodeJS学习笔记

      本文链接:https://www.haomeiwen.com/subject/mxpdqqtx.html