安装NodeJs入门来学习
1、
service.js 服务器模块运行
var http = require("http");//require('http') 总是返回内置的 HTTP 模块,即使该名称的文件存在。
//require用于请求加载模块文件。
http.createServer(function(request,response){
response.writeHead(200,{"Content-Type":"text/plain"});//200正常返回页面,内容text/plain
response.write("Hello World");
response.end();
}).listen(8888);
application/xml 、 text/xml、text/html、text/plain的区别
1、text/html是html格式的正文
2、text/plain是无格式正文
3、text/xml忽略xml头所指定编码格式而默认采用us-ascii编码
4、application/xml会根据xml头指定的编码格式来编码:
//
核心模块
Node 有一些已编译成二进制的模块,这些模块将在本文档的其他地方详细介绍。
核心模块在 Node 源代码的 lib/
文件夹中定义。
使用 require()
时,核心模块总是优先加载。例如,require('http')
总是返回内置的 HTTP 模块,即使该名称的文件存在。
文件模块
如果没有找到确切的文件,Node 将尝试给所需的文件名添加 .js
后缀再加载,然后再尝试 .node
。
.js
文件被视为 JavaScript 文本文件,而 .node
文件被视为已编译的插件模块,用 dlopen
加载。
模块以 '/'
开头表示使用文件的绝对路径。例如,require('/home/marco/foo.js')
将加载/home/marco/foo.js
文件。
模块以 './'
开头表示调用 require()
时使用相对路径。也就是说,为了保证foo.js
文件中的require('./circle')
能找到,circle.js
必须和foo.js
在同一目录。
如果不以 '/' 或'./' 开头,该模块可以是一个“核心模块”,也可是一个从 node_modules
文件夹中加载的模块。
接下来 运行
服务器模块
service.js
cmd 中运行 前提是配置好nodejsp环境变量
node service.js
浏览器 输入 http://localhost:8888/打开
显示
Hello Worldlisten(8888)可以加上地址变成 listen(8888,"127.0.0.1")
require("http") 获取http模块,并赋值给http变量。
http模块提供createServer()函数,会返回对象,并且该对象可以设置监听地址和端口, 可以只设置端口。