#4 NodeJS http 模块


工作目录

server.js

var http = require('http');
var fs = require('fs');
var path = require('path');
var mime = require('mime');
function send404(response){
     response.writeHead(404,{
          'Content-Type':'text/plain'
     });
     response.write('Error 404 : resource not found.');
     response.end();
}
function sendFile(request,response,filePath){
     fs.exists(filePath,function(exists){
          if(!exists){
               return send404(response);
          }
          fs.readFile(filePath,function(err,data){
               if(err) send404(response);
               response.writeHead(200,     {
                    'content-type':mime.lookup(path.basename(filePath))
               });
               response.end(data);
          })
     })
}
var server = http.createServer(function(request,response){
     var filePath = '';
     if(request.url == '/'){
          filePath = 'public/index.html';
     }else{
          filePath = './public' + request.url;
     }
     sendFile(request,response,filePath);
});
server.listen(3000,function(){
     console.log('Server listening on 3000');

})

index.html

按住Shift键不放,空白处右键,选择Open command windows here

执行下图命令

看到下图收摊


NodeJS http 模块的更多相关文章

  1. nodejs事件模块

    nodejs 事件模块 events 只有一个对象 EventEmitter . var EventEmitter = require('events').EventEmitter;var life ...

  2. 配置 Windows 下的 nodejs C++ 模块编译环境

    根据 node-gyp 指示的 Windows 编译环境说明, 简单一句话就是 "Python + VC++ 编译环境". 所有需要的安装文件, 我都下载好放到百度云盘了: nod ...

  3. nodejs的模块系统(实例分析exprots和module.exprots)

    前言:工欲善其事,必先利其器.模块系统是nodejs组织管理代码的利器也是调用第三方代码的途径,本文将详细讲解nodejs的模块系统.在文章最后实例分析一下exprots和module.exprots ...

  4. nodejs cluster模块初探

    大家都知道nodejs是一个单进程单线程的服务器引擎,不管有多么的强大硬件,只能利用到单个CPU进行计算.所以,为了使用多核cpu来提高性能 就有了cluster,让node可以利用多核CPU实现并行 ...

  5. NodeJS Web模块

    NodeJS Web模块 本文介绍nodeJS的http模块的基本用法,实现简单服务器和客户端 经典Web架构 Client:客户端一般指浏览器,通过HTTP协议向服务器发送请求(request) S ...

  6. 配置 Windows 下的 nodejs C++ 模块编译环境 安装 node-gyp

    配置 Windows 下的 nodejs C++ 模块编译环境 根据 node-gyp 指示的 Windows 编译环境说明, 简单一句话就是 "Python + VC++ 编译环境&quo ...

  7. nodejs cheerio模块提取html页面内容

    nodejs cheerio模块提取html页面内容 1. nodejs cheerio模块提取html页面内容 1.1. 找到目标元素 1.2. 美化文本输出 1.3. 提取答案文本 1.4. 最终 ...

  8. es6 中的模块导入与nodejs 中模块的导入的异同!

    我们知道es6 的模块导入导出是通过import 和 export 来实现,而nodejs的模块导入导出是通过require 和module.exports 来实现,那么它们有什么异同吗? 请看如下: ...

  9. 开始学nodejs——net模块

    net模块的组成部分 详见 http://nodejs.cn/api/net.html 下面整理出了整个net模块的知识结构,和各个事件.方法.属性的用法 net.Server类 net.Socket ...

随机推荐

  1. Sql 数据引擎中删除用户名、密码信息

    SQl版本:Microsoft SQL Server 2008 R2 系统:Windows Server 2008 R2 Enterprise 删除文件为:SqlStudio.bin 删除星系路径:C ...

  2. 第 一 百 天上课 PHP TP框架 数据库修改和删除

    修改的三种方式 //造数组的方式修改 public function xiugai1() { $db=D('yonghu'); $attr=array ( 'zhanghao'=>001, // ...

  3. avalon2学习教程13组件使用

    avalon2最引以为豪的东西是,终于有一套强大的类Web Component的组件系统.这个组件系统媲美于React的JSX,并且能更好地控制子组件的传参. avalon自诞生以来,就一直探索如何优 ...

  4. 表格制作模块xlwt

    import xlwtworkbook = xlwt.Workbook(encoding = 'ascii') #创建workbook 括号内容视情况而定sheetname = 'Sheet'book ...

  5. asp.net截取指定长度的字符串内容

    /// <summary> /// 用于截取指定长度的字符串内容 /// </summary> /// <param name="sString"&g ...

  6. ffmpeg未整理好,有时间整理下

    v  容器(Container) v  容器就是一种文件(封装)格式,比如flv.mkv.ts.mp4.rmvb.avi等.包含下面5种流以及文件头信息. v  流(Stream) v  是一种视频数 ...

  7. redis密码管理

    redis 默认密码是空,在应用中,通常需要设置redis的连接密码,可通过命名方式进行密码管理: 1.连接redis: [redis@hadooptest Downloads]$ cd redis- ...

  8. oracle 同时更新(update)多个字段多个值

    --创建表A,B: create table A (a1 varchar2(33),a2 varchar2(33),a3 varchar2(33)); create table B (b1 varch ...

  9. .net批量插入SqlServer数据库的方法:

    using System;using System.Collections.Generic;using System.Configuration;using System.Data;using Sys ...

  10. [Python] 关于64位机的numpy安装问题

    最近刚换成64位的系统,重新安装了win10,VS也从原来的2010变为了现在的2013. 利用原来32位电脑硬盘里的python2.7安装包安装,然后打算安装numpy. 上来碰到问题:在windo ...