zoukankan      html  css  js  c++  java
  • 第一个超级简单Node.js实例

    这些天有事没事的稍微看看Node.js,觉得还是挺有意思的,这里自己做一个简单的例子,起码能让没玩过的人觉得Node.js没那么神秘。

    1.下载安装Node.js for windows,安装。http://nodejs.org

    2.CMD到安装根目录下安装模块,expressnpm install express,还可以安装mysql:npm install mysql,以及mangodb:npm install mongodb

    3.这样环境就做好了(简单示例就不使用其他的IDE了),在根目录下新建一个hello.js文件,复制代码如下:

    var http = require('http');
    var url = require('url');
    
    http.createServer(function (req, res) {        
          var path = url.parse(req.url).pathname;
    		var dt=new Date();
            res.writeHead(200, {'Content-Type': 'text/plain'});
            res.write("Hello,World ! \n"+dt.getTime());
            res.end();}).listen(8888, "127.0.0.1");
     
    console.log('access:http://localhost:8888');
    

    4.启动我们的实例,CMD到hello.js目录输入(Node的子目录下可直接使用node命令,根目录执行文件须使用完整的路径文件名C:\Program Files\nodejs\JS>node "C:\Program Files\nodejs\JS\hw.js"):

    node hello.js

    在浏览器中输入地址:localhost:8888

    刷新页面看到时间的变化。

    其他的不多说,代码解释有很多文章介绍,这个是玩Node.js要了解的最基本的东西。

    后面还会有文章介绍,敬请期待。

  • 相关阅读:
    对象直接量
    js学习类
    jquery.js与sea.js综合使用
    拥抱模块化的JavaScript
    匿名函数与闭包
    js对象如何合并?
    Web.config配置文件详解
    javascipt自定义命名空间、静态类、实例对象
    jQuery源码的基础知识
    企业架构/企业开发 [Enterprise architecture / Enterprise Development]
  • 原文地址:https://www.cnblogs.com/wancy86/p/nodejs_sample.html
Copyright © 2011-2022 走看看