码迷,mamicode.com
首页 > Web开发 > 详细

NodeJS

时间:2019-03-30 01:08:02      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:安装   res   bfc   node   app.js   index   传值   app   设置   

安装

https://nodejs.org/en/download/

自动启动工具

安装

npm install -g supervisor

启动

supervisor app.js

http模块

技术图片
const http = require(http)

http.createServer((req, res) => {
  res.writeHead(200,{Content-Type:text/html;charset=utf-8})
  res.end(Hello World!)
}).listen(3000)
View Code

我们可以看出NodeJS不及是一个应用还是一个HTTP服务器

url模块

url.parse()

技术图片
url.parse(http://localhost:3000)
Url {
  protocol: http:,
  slashes: true,
  auth: null,
  host: localhost:3000,
  port: 3000,
  hostname: localhost,
  hash: null,
  search: null,
  query: null,
  pathname: /,
  path: /,
  href: http://localhost:3000/ }
> url.parse(http://localhost:3000?name=Susan&age=18)
Url {
  protocol: http:,
  slashes: true,
  auth: null,
  host: localhost:3000,
  port: 3000,
  hostname: localhost,
  hash: null,
  search: ?name=Susan&age=18,
  query: name=Susan&age=18,
  pathname: /,
  path: /?name=Susan&age=18,
  href: http://localhost:3000/?name=Susan&age=18 }
> url.parse(http://localhost:3000?name=Susan&age=18, true)
Url {
  protocol: http:,
  slashes: true,
  auth: null,
  host: localhost:3000,
  port: 3000,
  hostname: localhost,
  hash: null,
  search: ?name=Susan&age=18,
  query: [Object: null prototype] { name: Susan, age: 18 },
  pathname: /,
  path: /?name=Susan&age=18,
  href: http://localhost:3000/?name=Susan&age=18 }
View Code

代码

const query = url.parse(req.url, true).query  // 设置为true,get传值转换为对象,如果不设置true,那么是字符串

url.resolve()

技术图片
> url.resolve(http://localhost:3000, index)
http://localhost:3000/index
> url.resolve(http://localhost:3000/home, index)
View Code

 

NodeJS

标签:安装   res   bfc   node   app.js   index   传值   app   设置   

原文地址:https://www.cnblogs.com/sonwrain/p/10624967.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!