zoukankan      html  css  js  c++  java
  • Node创建应用

    github地址:https://github.com/lily1010/Node_learn/tree/master/test

    一 使用node的意义

    使用 Node.js 时,我们不仅仅 在实现一个JS应用,同时还实现了整个 HTTP 服务器.

    二 Node.js 应用是由哪几部分组成的

    (1)引入 required 模块:我们可以使用 require 指令来载入 Node.js 模块.

    (2)创建服务器:服务器可以监听客户端的请求,类似于 Apache 、Nginx 等 HTTP 服务器.

    (3)接收请求与响应请求 服务器很容易创建,客户端可以使用浏览器或终端发送 HTTP 请求,服务器接收请求后返回响应数据.

    三 创建应用例子

    先建个example的js文件,写入:

    //步骤一、引入 required 模块:我们使用 require 指令来载入 http 模块,并将实例化的 HTTP 赋值给变量 http,实例如下:
    var http = require('http');   //第一行请求(require)Node.js 自带的 http 模块,并且把它赋值给 http 变量。
    //步骤二、创建服务器:使用 http.createServer() 方法创建服务器,并使用 listen 方法绑定 10086 端口。 函数通过 request, response 参数来接收和响应数据
    http.createServer(function(req,res){
        res.writeHead(200,{'Content-type':'text/plain'});
        res.end('Hello Node
    ');
    }).listen(10086,'127.1.1.1'); //接下来我们调用 http 模块提供的函数: createServer 。这个函数会返回 一个对象,这个对象有一个叫做 listen 的方法,这个方法有一个数值参数, 指定这个 HTTP 服务器监听的端口号。
    
    console.log('Server running at http://127.1.1.1:10086/');

    然后执行 node example.js

    从上图显示看出Server running at http://127.1.1.1:10086/

    然后打开浏览器输入http://127.1.1.1:10086/,就可以看到页面输出hello Node了

  • 相关阅读:
    nested exception is java.lang.IllegalStateException: No persistence units parsed from {classpath*:META-INF/persistence.xml}
    Thrift Expected protocol id ffffff82 but got 0
    idea
    Activity工作流入门之HelloWorld
    Thrift 入门之helloWorld
    Thrift入门之mac下的安装流程
    netty的解码器与粘包和拆包
    java反射(一)
    使用Spring报错:No default constructor found;
    jpa关联映射(一)
  • 原文地址:https://www.cnblogs.com/lily1010/p/5865019.html
Copyright © 2011-2022 走看看