标签:安装 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)
我们可以看出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‘ }
代码
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‘)
标签:安装 res bfc node app.js index 传值 app 设置
原文地址:https://www.cnblogs.com/sonwrain/p/10624967.html