直接上代码吧,我把它命名为 app.js, 只要在该文件所在目录下,控制台运行 node app.js 即可启动一个本地服务器了。

/**
* 服务器
* Author jervy
* Date
*/ var MINE_TYPES = {
'html': 'text/html',
'xml': 'text/xml',
'txt': 'text/plain',
'css': 'text/css',
'js': 'text/javascript',
'json': 'application/json',
'pdf': 'application/pdf',
'swf': 'application/x-shockwave-flash',
'svg': 'image/svg+xml',
'tiff': 'image/tiff',
'png': 'image/png',
'gif': 'image/gif',
'ico': 'image/x-icon',
'jpg': 'image/jpeg',
'jpeg': 'image/jpeg',
'wav': 'audio/x-wav',
'wma': 'audio/x-ms-wma',
'wmv': 'video/x-ms-wmv',
'woff2': 'application/font-woff2'
};
var PORT = 3000;
var http = require('http');
var url = require('url');
var fs = require('fs');
var path = require('path');
var root = process.cwd(); var server = http.createServer(function(request, response) {
var pathname = decodeURIComponent(url.parse(request.url).pathname);
var realPath = path.join(root, pathname);
var ext = path.extname(realPath);
if (!ext) {
realPath = path.join(realPath, '/index.html');
ext = '.html'
}
fs.exists(realPath, function(exists) {
if (exists) {
fs.readFile(realPath, 'binary', function(err, file) {
if (!err) {
response.writeHead(200, {
'Content-Type': MINE_TYPES[ext.slice(1)] || 'text/plain'
});
response.write(file, 'binary');
response.end();
} else {
response.writeHead(500, {
'Content-Type': 'text/plain'
});
response.write('ERROR, the reason of error is ' + err.code + '; Error number is ' + err.errno + '.');
response.end();
}
})
} else {
response.writeHead(404, {
'Content-Type': 'text/plain'
});
response.write('This request URL ' + pathname + ' was not found on this server.');
response.end();
}
}); });
server.listen(PORT);
console.log("server running at port " + PORT);

用node写个简单的静态服务器的更多相关文章

  1. 用Node.js原生代码实现静态服务器

    ---恢复内容开始--- 后端中服务器类型有两种 1. web服务器[ 静态服务器 ] - 举例: wamp里面www目录 - 目的是为了展示页面内容 - 前端: nginx 2. 应用级服务器[ a ...

  2. 如何用PHP/MySQL为 iOS App 写一个简单的web服务器(译) PART1

    原文:http://www.raywenderlich.com/2941/how-to-write-a-simple-phpmysql-web-service-for-an-ios-app 作为一个i ...

  3. 用node.js实现简单的web服务器

    node.js实现web服务器还是比较简单的,我了解node.js是从<node入门>开始的,如果你不了解node.js也可以看看! 我根据那书一步一步的练习完了,也的确大概了解了node ...

  4. 如何写一个简单的http服务器

    最近几天用C++写了一个简单的HTTP服务器,作为学习网络编程和Linux环境编程的练手项目,这篇文章记录我在写一个HTTP服务器过程中遇到的问题和学习到的知识. 服务器的源代码放在Github. H ...

  5. 如何写一个简单的HTTP服务器(重做版)

    最近几天用C++重新写了之前的HTTP服务器,对以前的代码进行改进.新的HTTP服务器采用Reactor模式,有多个线程并且每个线程有一个EventLoop,主程序将任务分发到每个线程,其中采用的是轮 ...

  6. iOS 使用node js 搭建简单的本地服务器

    一.前提:基于iOS 项目 调用,使用了第三方框架NodeMobile.技术说明关键是 应用生命整个周期只能在应用启动时候开辟的一个线程里申请 一个 node  js 资源.如果终止了运行,重启是不支 ...

  7. python写一些简单的tcp服务器和客户端

    代码贴上,做个记录 TcpClient # -*- coding:utf-8 -*- import socket target_host = "127.0.0.1" #服务器端地址 ...

  8. 使用node写一个简单的页面操作

    let http = require('http'); let urlStr = require('url'); let fs = require('fs'); let path = require( ...

  9. nodejs写一个简单的Web服务器

    目录文件如 httpFile.js如下: const httpd = require("http"); const fs = require("fs"); // ...

随机推荐

  1. String 对象

    1 你使用位置(索引)可以访问字符串中任何的字符: var str="this is a demo"; alert(str[3])//    s 字符串的索引从零开始, 所以字符串 ...

  2. bzoj2660

    dp 看了挺长时间的,这篇写的很好:http://97littleleaf11.xyz/oi/bzoj-2660/ 我们先把n按照斐波那契数列贪心分解,然后发现可以把现在组合的斐波那契数分解成两个较小 ...

  3. git合并相关问题(copy)

    [说明:资料来自http://gitbook.liuhui998.com/3_3.html] 一个Git仓库可以维护很多开发分支.现在我们来创建一个新的叫”experimental”的分支: $ gi ...

  4. bzoj 1951: [Sdoi2010]古代猪文 【中国剩余定理+欧拉定理+组合数学+卢卡斯定理】

    首先化简,题目要求的是 \[ G^{\sum_{i|n}C_{n}^{i}}\%p \] 对于乘方形式快速幂就行了,因为p是质数,所以可以用欧拉定理 \[ G^{\sum_{i|n}C_{n}^{i} ...

  5. A joke about regular expression

    As the old computer science joke goes: “Let’s say you have a problem, andyou decide to solve it with ...

  6. ueditor 编辑

    1.net  config.json imageUrlPrefix

  7. Miller&&Pollard HDOJ 4344 Mark the Rope

    题目传送门 题意:一个长为n(n<2^63)的管子,在管子上做标记,每隔L个长度单位做一个标记,从管子头端开始,保证最后一次标记恰好在管子的尾端.让你找出有多少个这样的L(L<n),且他们 ...

  8. ACM_给你100块钱

    给你100块钱 Time Limit: 2000/1000ms (Java/Others) Problem Description: 小光见到昨晚旭能神没拿到一血,又损失了一百块,很同情他.但是为了不 ...

  9. d3学习笔记

    (1)使用enter()函数. 当要创建新的绑定数据的元素,必须使用enter().这个方法会分析当前选择的DOM元素和传给它的数据,如果数据值比对应的DOM元素多,就创建一个新的占位元素.然后把这个 ...

  10. hibernate 中createQuery与createSQLQuery(转载)

    息: java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.miracle.dm.doc.catalog.mo ...