多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# 基础知识 nodejs第一个程序,运行 node 1.js ``` <pre class="calibre14">``` var http <span class="token">=</span> <span class="token1">require</span><span class="token2">(</span><span class="token4">"http"</span><span class="token2">)</span><span class="token2">;</span> http<span class="token2">.</span><span class="token1">createServer</span><span class="token2">(</span><span class="token5">function</span> <span class="token2">(</span>a<span class="token2">,</span>b<span class="token2">)</span> <span class="token2">{</span> b<span class="token2">.</span><span class="token1">writeHead</span><span class="token2">(</span><span class="token3">200</span><span class="token2">,</span><span class="token2">{</span><span class="token4">'content-type'</span><span class="token2">:</span><span class="token4">'text/html'</span><span class="token2">}</span><span class="token2">)</span><span class="token2">;</span> b<span class="token2">.</span><span class="token1">write</span><span class="token2">(</span><span class="token4">"<meta charset='utf-8' />"</span><span class="token2">)</span><span class="token2">;</span> b<span class="token2">.</span><span class="token1">end</span><span class="token2">(</span><span class="token4">"你好测试100分"</span><span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span><span class="token2">)</span><span class="token2">.</span><span class="token1">listen</span><span class="token2">(</span><span class="token3">8080</span><span class="token2">,</span><span class="token4">'127.0.0.1'</span><span class="token2">)</span><span class="token2">;</span> console<span class="token2">.</span><span class="token1">log</span><span class="token2">(</span><span class="token4">"正在运行中"</span><span class="token2">)</span><span class="token2">;</span> ``` ``` 文件模块 ``` <pre class="calibre14">``` var fs <span class="token">=</span> <span class="token1">require</span><span class="token2">(</span><span class="token4">'fs'</span><span class="token2">)</span><span class="token2">;</span><span class="token6">//读取文件</span> var ss <span class="token">=</span> fs<span class="token2">.</span><span class="token1">readFileSync</span><span class="token2">(</span><span class="token4">'1.txt'</span><span class="token2">,</span><span class="token4">'utf8'</span><span class="token2">)</span><span class="token2">;</span><span class="token6">//同步读取</span> fs<span class="token2">.</span><span class="token1">readFile</span><span class="token2">(</span><span class="token4">'1.txt'</span><span class="token2">,</span><span class="token4">'utf8'</span><span class="token2">,</span><span class="token5">function</span> <span class="token2">(</span>err<span class="token2">,</span>rs<span class="token2">)</span> <span class="token2">{</span> <span class="token6">//异步输出</span> <span class="token2">}</span><span class="token2">)</span><span class="token2">;</span> ``` ``` 定义模块 ``` <pre class="calibre14">``` exports<span class="token2">.</span>getname <span class="token">=</span> <span class="token5">function</span> <span class="token2">(</span>names<span class="token2">)</span> <span class="token2">{</span> <span class="token5">return</span> names<span class="token2">;</span> <span class="token2">}</span> 使用 var my <span class="token">=</span> <span class="token1">require</span><span class="token2">(</span><span class="token4">'./2'</span><span class="token2">)</span><span class="token2">;</span> my<span class="token2">.</span><span class="token1">getname</span><span class="token2">(</span><span class="token4">"123"</span><span class="token2">)</span> ``` ``` 常用模块 until 常用函数集合 events 事件驱动 fs 文件系统 http 服务器