初识nodejs

一:第一个helloword

1.创建文件(d盘/524nodejs),在文件下创建js文件(hello.js),js内的代码如下:

2.运行nodejs程序:(1)更改目录到当前运行的目录下面:cd: d:/524nodejs(如果没有到,返回上级目录:cd ../);

                           (2). 运行代码:node hello.js(可以到127.0.0.1:8888进行查看是否运行成功

1 var http = require('http');//引入require模块
2 http.createServer(function(request,response){//通过createServer()方法创建服务器,端口号8888
         // 发送 HTTP 头部 
	// HTTP 状态值: 200 : OK
	// 内容类型: text/plain(文本类型)
3 response.writeHead(200,{'Content-Type':'text/plain'}); 
4 response.end("hello word");
5 }).listen(8888);
6 console.log('Server running at http://127.0.0.1:8888');
原文地址:https://www.cnblogs.com/changyuqing/p/5522889.html