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.模块路径解析规则
项目目录
其中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 [email protected]
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": {
"[email protected]@silly-datetime": "^0.1.2",
"silly-datetime": "^0.1.2"
}
^ 表示第一版本号不变,后面两位取最新的
~ 表示前两位不变,最后一个取最新
* 表示全部取最新