兄弟会精英班 - 学习笔记(五)

Nodejs的基本语法

Nodejs Js的编译运行环境,运行在服务器端的 JavaScript,事件驱动I/O服务端JS运行环境,
基于Google的V8引擎,执行JS的速度非常快,性能非常好。

Js基础知识学习参考 W3cSchool

  • Vim创建test.js的nodejs文件
    var name = "Hello World!"; console.log(name);

服务器端执行 node test.js , 屏幕上打印出 Hello World!,

  • if 流程控制

if ( ) { }
else if ( ) { }
else { }

  • for 循环语句

for (var i; i < 10; i++) { }

  • switch case 语句

switch ( n )
{
case 1:
执行代码 1
break;
case 2:
....
default:
}

创建http服务器

  • 引入Nodejs的核心模块http

const http = require("http"); //引入http模块
const ip = ""; //设置服务器IP地址
const port = ; //设置服务器端口

  • 通过res, rep 参数来接收和响应数据

var a =function(req, res) {
res.writeHead(200, {'content':'text/html'} ); //发送http头部,http状态值200,内容类型text/html
res.end("Hello World \n"); //发送相应数据Hello World
}

  • createServer创建http服务器

var server = http.createServer( a );

  • 终端打印信息

var c = function(){
console.log("Server is running at http://", http);
}

  • 监听端口

server.listen( port, ip, c);

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容