NodeJS http 模块
#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 模块的更多相关文章
- nodejs事件模块
nodejs 事件模块 events 只有一个对象 EventEmitter . var EventEmitter = require('events').EventEmitter;var life ...
- 配置 Windows 下的 nodejs C++ 模块编译环境
根据 node-gyp 指示的 Windows 编译环境说明, 简单一句话就是 "Python + VC++ 编译环境". 所有需要的安装文件, 我都下载好放到百度云盘了: nod ...
- nodejs的模块系统(实例分析exprots和module.exprots)
前言:工欲善其事,必先利其器.模块系统是nodejs组织管理代码的利器也是调用第三方代码的途径,本文将详细讲解nodejs的模块系统.在文章最后实例分析一下exprots和module.exprots ...
- nodejs cluster模块初探
大家都知道nodejs是一个单进程单线程的服务器引擎,不管有多么的强大硬件,只能利用到单个CPU进行计算.所以,为了使用多核cpu来提高性能 就有了cluster,让node可以利用多核CPU实现并行 ...
- NodeJS Web模块
NodeJS Web模块 本文介绍nodeJS的http模块的基本用法,实现简单服务器和客户端 经典Web架构 Client:客户端一般指浏览器,通过HTTP协议向服务器发送请求(request) S ...
- 配置 Windows 下的 nodejs C++ 模块编译环境 安装 node-gyp
配置 Windows 下的 nodejs C++ 模块编译环境 根据 node-gyp 指示的 Windows 编译环境说明, 简单一句话就是 "Python + VC++ 编译环境&quo ...
- nodejs cheerio模块提取html页面内容
nodejs cheerio模块提取html页面内容 1. nodejs cheerio模块提取html页面内容 1.1. 找到目标元素 1.2. 美化文本输出 1.3. 提取答案文本 1.4. 最终 ...
- es6 中的模块导入与nodejs 中模块的导入的异同!
我们知道es6 的模块导入导出是通过import 和 export 来实现,而nodejs的模块导入导出是通过require 和module.exports 来实现,那么它们有什么异同吗? 请看如下: ...
- 开始学nodejs——net模块
net模块的组成部分 详见 http://nodejs.cn/api/net.html 下面整理出了整个net模块的知识结构,和各个事件.方法.属性的用法 net.Server类 net.Socket ...
随机推荐
- centos6.x下手工安装二进制Docker v1.1x
Docker在 centos 6.x 下面默认最新的版本是1.7, 然而这个并不符合我的实际需求, 尤其我需要 docker-compose 来作为编配工具部署swarm, 所以只有使用二进制的安装包 ...
- AX2012 DMF数据导入的问题
由于AX2012的数据结构比较复杂,通过Excel直接导入表的方式很多数据已经难以导入,比如物料信息,2009只需要导入InventTable,InventTableModule和InventItem ...
- 使用阿里Docker镜像加速器加速
在阿里开发者平台注册开发者账号 https://dev.aliyun.com/search.html 注册之后可以访问Docker镜像服务 https://cr.console.aliyun.com/ ...
- spring结合Quartz的集群功能实现
一:前沿 在上一篇(http://www.cnblogs.com/wuhao1991/p/4331613.html)的博客中记载了定时的功能,但是集成是没有成功的,在这篇中,我在解释下这里的”集成的含 ...
- AngularJS学习--- 动画操作 (Applying Animations) ngAnimate step 12
1.切换目录 git checkout step-12 npm start 2.效果图 这里在点击右边的缩略图时,会有一个很明显的从下向上的动画过程. 3.代码实现: step11和step12之间的 ...
- 解决org.openqa.selenium.WebDriverException: Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms org.springframework.beans.BeanInstantiation
解决方法为将selenium-server-standalone-2.37.0.jar升级至selenium-server-standalone-2.41.0.jar即可. 下载地址:http://s ...
- php 图片处理类
<?php /** * 图片类 * @author <420012223@qq.cn> */ class Image { public $uploadImagePath = './t ...
- [MyBean-插件]MyBean通用报表免费无限制版本发布
[优点] 1.开发时无需安装报表组件(可以直接用编译好的文件,注意版权说明,请自行编译一次相应的报表插件文件). 2.无带包烦恼所有版本Delphi都可以使用,不拖累Delphi版本的 ...
- Eclipse报错:Setting property 'source' to 'org.eclipse.jst.jee.server:test1' did no
最近把Eclipse的maven插件从m2eclipse更新到m2e后出了一些莫名其妙的的问题.今天又出了一个,就是Eclipse新建的Maven Web project在tomcat里启动后报错,具 ...
- LeetCode(84) Largest Rectangle in Histogram
题目 Given n non-negative integers representing the histogram’s bar height where the width of each bar ...