安装
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
我们可以看出 Node.JS 不及是一个应用还是一个 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
- url.resolve()
- > url.resolve('http://localhost:3000', 'index')
- 'http://localhost:3000/index'
- > url.resolve('http://localhost:3000/home', 'index')
- View Code
来源: http://www.bubuko.com/infodetail-3004627.html