美文网首页
2016.09.09 如何创建nodejs HTTP服务器

2016.09.09 如何创建nodejs HTTP服务器

作者: Success85 | 来源:发表于2016-09-10 22:03 被阅读48次

基本流程

  1. 首先:引入node的核心模块http:require("http");
  2. 通过http的createServer创建.http.server的一个实例,在里面可以输入你要输出的一些信息,
  3. 使用http.server的实例的listen的方法。监听ip和端口,并传入一个回调函数

示例

const http = require('http');//调用http模块
const ip = "192.168.238.128";//定义服务器的IP
const port = 3000;//定义端口
var a = function(req,res){
    res.writeHead(200,{'content-type':'text/html'});
    console.log(res);
    res.write('dajiabuyaofuzao');
    res.end('bu yao fu zao');
}
var server = http.createServer(a);
var c = function(){
    console.log('server is running');
}
server.listen(port,ip,c);

nodejs HTTP服务器请求返回参数

  1. statusMessage 状态信息
  2. 普通
    Request URL:请求的网址
    Request Method:请求方式
    Status Code: 返回状态码
    Remote Address:请求地址及端口
  3. 请求头信息
    host: '192.168.235.128:6666',
    connection: 'keep-alive',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36',
    accept: '/',
    referer: 'http://192.168.235.128:6666/',
    'accept-encoding': 'gzip, deflate, sdch',
    'accept-language': 'zh-CN,zh;q=0.8,en;q=0.6'
  4. 响应头信息
    Connection:close 链接状态
    Content-Type:text/html 返回类型
    Date:Sun, 11 Sep 2016 12:43:59 GMT 返回时间
    Server:Apache/2.2.3 (Red Hat) 服务器信息
    Transfer-Encoding:chunked
    X-Powered-By:PHP/4.4.9 版权信息
    Cache-control:private
    Content-Encoding:gzip
    Content-Type:text/html; charset=utf-8
    Date:Sun, 11 Sep 2016 12:50:17 GMT
    Expires:Thu, 19 Nov 1981 08:52:00 GMT
    Keep-Alive:timeout=5, max=99
    Pragma:no-cache
    Server:Apache/2.4.18 (Win32) OpenSSL/1.0.2e mod_fcgid/2.3.9
    Set-Cookie:PHPSESSID=okc2bq1i1rpi0bhq0njoqgc591; path=/
    Set-Cookie:ZDEDebuggerPresent=php,phtml,php3; path=/
    Transfer-Encoding:chunked
    Vary:Accept-Encoding

作业(nodejs)

1、打印一个空心的菱形
2、打印一个空心的梯形
3、打印一个回形
4、字符串每3个加逗号分隔
5、创建一个http服务,把用户请求整理出来

相关文章

  • 2016.09.09 如何创建nodejs HTTP服务器

    基本流程 首先:引入node的核心模块http:require("http"); 通过http的createSer...

  • nodejs模块

    nodejs模块 nodejs系统自带的模块:http:协议请求模块;创建服务器:http.createServe...

  • 2018-11-29

    手把手带你NodeJs入门(一) http 创建服务器 创建一个简单的服务器,在nodejs中创建一个server...

  • nodejs静态资源服务器

    缩写含义 http是nodejs的服务模块 url是url路由模块 fs是文件服务器模块 nodejs服务器的创建...

  • nodejs系列-HTTP

    使用nodejs可以快速的创建一个HTTP服务器,使用过webpack的,对于nodejs服务器并不陌生,我们在开...

  • nodejs HTTP服务器创建

    用惯了express框架的你是否还记得怎么样用nodejs自己的http模块来创建一个服务器吗,这也是很多大公司笔...

  • XDH_LESSON4

    nodeJS 静态服务器简单的服务器 在WM虚拟机ubuntu系统下 创建一个服务器。 const http=re...

  • 手写一个后端服务器

    一、nodejs写服务器 var http = require('http') //是nodejs的内置模块 - ...

  • 快速入门nodejs

    安装不讲 输出nodejs test.js 引入 required 模块 创建一个Http服务器server.js...

  • node.js学习日记day1

    nodejs-01 http模块 创建一个http服务器: url模块中的parse方法: 该方法可以把url中包...

网友评论

      本文标题:2016.09.09 如何创建nodejs HTTP服务器

      本文链接:https://www.haomeiwen.com/subject/exxuettx.html