01. Node js Hello world

01. Node js Hello world

环境安装

官网http://nodejs.org/

Node.js is a platform built on Chrome's JavaScript runtime for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.

上面那句话来自官网;本身就是一个网络应用;不像有些是本地应用

进入官网,看到install 按钮直接点去,就会自动去下载;下载完后自动去安装;

  • 确保 node 命令在Path 目录下

  • 在terminal 中运行

    node -v
    

    看到 v0.10.26 类似的结果就说明安装成功

写个Hello World

  1. hello_world.js

    var http = require('http')
    http.createServer(function(req, res){
        res.writeHead(200,{'Content-Type':'text/plain'});
        res.end('Hello world\n');
    }).listen(1337,'127.0.0.1');
    console.log('Server running at http://127.0.0.1');
    
  2. 运行

    node hello_world.js
    
  3. 浏览器输入

    http://127.0.0.1:1337
    
  4. 真正的Hello world
    在hello_world.js中如下:

    console.log('Hello world')
    

    运行:

    node hello_world.js
    
  5. 其实还有更简单的

    直接在終端输入:

    node
    

    就出现命令行界面, 直接输入

    console.log('Hello world')
    

以上是入门,基础;

你可能感兴趣的:(01. Node js Hello world)