node如何实现网址的伪静态或者二级目录伪静态模式

nodejs如何设置静态目录!不用express.纯nodejs实现! - CNode技术社区
hi!一起学习nodejs
我的需求是这样的.我有一个目录,里面全是html文件.比如,stc目录(这个目录中有a.html,b.html),当用户访问 xxx.con/stc/a.html
就能访问到这个页面.但是当用户访问c.html的页面时就提示他不存在或者返回404, 如何实现呢?是不是要遍历这个stc目录啊,然后跟req.url去比较,但是感觉效率很低啊!!!? 很疑惑.请知道的朋友不吝赐教!
用nginx就行了,跟node没关系
node不能实现吗? 我有几千个html文件…╮(╯▽╰)╭
直接用http模块写要写好多代码,干嘛要重复造轮子嘛。如果想要个nginx的替代品可以试试redbird
如果只是想要个静态目录的web服务器,可以试试http-server
nginx不就行了嘛。。。
node是可以实现,用自己原生的http模块就可以,只不过没必要,你需要的是一个http服务器,而不是一个服务引擎,有好多http服务器在这方面都做到了极致,高性能、易部署、超稳定,nginx、lighttp、apache等等都可以呀,为什么一定要用node来写。
以前写过一个纯node.js的静态服务器,希望对你有帮助。
我就是生成一批页面自己用的.没必要用nginx的!
简单说吧,反正横竖都要用http服务器,使用现成的http服务器还是自己用node写一个?你用现成的HTTP服务器nginx的话只需要两行配置,用node自己写一个http服务器的话要写几十行,功能还不全,性能比nginx也差很多,你自己选咯。
var http = require('http');
var fs = require('fs');
var path = require('path');
http.createServer(function (request, response) {
console.log('request starting...');
var filePath = '.' + request.
if (filePath == './')
filePath = './index.html';
var extname = path.extname(filePath);
var contentType = 'text/html';
switch (extname) {
case '.js':
contentType = 'text/javascript';
case '.css':
contentType = 'text/css';
case '.json':
contentType = 'application/json';
case '.png':
contentType = 'image/png';
case '.jpg':
contentType = 'image/jpg';
case '.wav':
contentType = 'audio/wav';
fs.readFile(filePath, function(error, content) {
if (error) {
if(error.code == 'ENOENT'){
fs.readFile('./404.html', function(error, content) {
response.writeHead(200, { 'Content-Type': contentType });
response.end(content, 'utf-8');
response.writeHead(500);
response.end('Sorry, check with the site admin for error: '+error.code+' ..\n');
response.end();
response.writeHead(200, { 'Content-Type': contentType });
response.end(content, 'utf-8');
}).listen(8125);
console.log('Server running at http://127.0.0.1:8125/');
如果你想跨域访问的话还要加header:
// Website you wish to allow to connect
response.setHeader('Access-Control-Allow-Origin', '*');
// Request methods you wish to allow
response.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
// Request headers you wish to allow
response.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
response.setHeader('Access-Control-Allow-Credentials', true);
不想重新造轮子,可以用https://github.com/expressjs/serve-static ,其内部做的事情和楼上说的差不多
CNode 社区为国内最专业的 Node.js 开源技术社区,致力于 Node.js 的技术研究。
服务器赞助商为
,存储赞助商为
,由提供应用性能服务。
新手搭建 Node.js 服务器,推荐使用无需备案的伪静态web&config配置步骤
web.config是asp.net一个重要的配置文件,本文将介绍如何利用web.config配置伪静态,步骤很详细,需要了解的朋友可以参考下
&?xml version="1.0" encoding="UTF-8"?&
&configuration&
&!--第一步注册url重写模块放到webconfig的最上部--&
&configSections&
&section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" /&
&/configSections&
&!--第二步添加重写规则--&
&RewriterConfig&
&!--先制定全部重写规则内容--&
&!--制定每个单独页面的规则--&
&RewriterRule&
&!--原始请求地址--&
&SendTo&&![CDATA[~/news.aspx?id=$1&pid=$2]]&&/SendTo&
&!--重写后的地址--&
&LookFor&~/news/(.[0-9]*)/(.[\d]*)\.html&/LookFor&
&/RewriterRule&
&RewriterRule&
&!--原始请求地址--&
&SendTo&&![CDATA[~/product.aspx?pid=$1]]&&/SendTo&
&!--重写后的地址--&
&LookFor&~/product/(.[\d]*)\.zangdalei&/LookFor&
&/RewriterRule&
&/RewriterConfig&
&system.web&
&compilation debug="false" targetFramework="4.0"&
&!--第四部url重写防止真实的页面也被重写如果网站中真实存在页面,需要添加编译指令不编译真实的html文件--&
&buildProviders&
&add extension=".html" type="System.Web.Compilation.PageBuildProvider" /&
&/buildProviders&
&/compilation&
&!--第三部url重写 将用户的请求页面交给相应的处理程序,注意请求的后缀名格式--&
&httpHandlers&
&add verb="*" path="*.html" type="URLRewriter.RewriterFactoryHandler, URLRewriter" /&
&add verb="*" path="*.zangdalei" type="URLRewriter.RewriterFactoryHandler, URLRewriter" /&
&/httpHandlers&
&/system.web&
&system.webServer&
&handlers&
&add name="aspnethtml" path="*.html" verb="GET,POST" modules="IsapiModule" scriptProcessor="C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" resourceType="Unspecified" preCondition="classicMode,runtimeVersionv4.0,bitness32" /&
&add name="aspnetzangdalei" path="*.zangdalei" verb="GET,POST" modules="IsapiModule" scriptProcessor="C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" resourceType="Unspecified" preCondition="classicMode,runtimeVersionv4.0,bitness32" /&
&/handlers&
&defaultDocument&
&add value="default.aspx" /&
&add value="default.html" /&
&/defaultDocument&
&/system.webServer&
&/configuration&
Copyright (C) , All Rights Reserved.
版权所有 闽ICP备号
processed in 0.039 (s). 12 q(s)推荐这篇日记的豆列
&&&&&&&&&&&&ThinkPHP教程 列表
相关参考课程ThinkPHP 利用.htaccess文件的 Rewrite 规则隐藏URL中的 index.php去掉 URL 中的 index.php
ThinkPHP 作为 PHP 框架,是单一入口的,那么其原始的 URL 便不是那么友好。但 ThinkPHP 提供了各种机制来定制需要的 URL 格式,配合 Apache .htaccess 文件,更是可以定制出人性化的更利于 SEO 的 URL 地址来。
.htaccess文件是 Apache 服务器中的一个配置文件,它负责相关目录下的网页配置。我们可以利用 .htaccess 文件的 Rewrite 规则来隐藏掉 ThinkPHP URL 中的 index.php 文件(即入口文件),这也是 ThinkPHP URL 伪静态的第一步。
例如原来的 URL 为:
http://www.5idev.com/index.php/Index/insert
去掉 index.php 之后变为:
http://www.5idev.com/Index/insert
如此一来,就变成了 http://服务器地址/应用模块名称/操作名称[/变量参数] 的常见 URL 格式。
更改 Apache httpd.conf 配置文件
提示:如果在虚拟主机商配置,请直接配置第三、四步,因为支持 .htaccess 的空间已经配置好了前面两步。
用编辑器打开 Apache 配置文件 httpd.conf(该文件位于 Apache 安装目录Apache2conf),并按如下步骤修改,。
一、加载了 mod_rewrite.so
确认加载了 mod_rewrite.so 模块(将如下配置前的 # 号去掉):
LoadModule rewrite_module modules/mod_rewrite.so
二、更改 AllowOverride 配置
更改需要读取 .htaccess 文件的目录,将原来的目录注释掉:
#&Directory &C:/Program Files/Apache Group/Apache2/htdocs&&
&Directory E:/html/myapp&
更改 AllowOverride None 为 AllowOverride FileInfo Options ,更改后的配置如下所示:
#&Directory &C:/Program Files/Apache Group/Apache2/htdocs&&
&Directory E:/html/myapp&
AllowOverride FileInfo Options
&/Directory&
.htaccess 是基于目录来控制的,&Directory E:/html/myapp& 该句即表示需要读取 .htaccess 文件的目录,要根据实际具体 Apache 的解析目录来配置。虚拟主机如果提供 .htaccess 控制,一般都已经配置好了。
三、添加 .htaccess 文件 Rewrite 规则
在需要隐藏 index.php 的目录下(本教程中为 E:/html/myapp,也即入口文件所在目录)创建 .htaccess 文件,并写入如下规则代码:
&IfModule mod_rewrite.c&
RewriteEngine on
#不显示index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
&/IfModule&
如果网站已经有 .htaccess 文件,在里面添加该段配置规则即可。如果不能创建该文件(Windows 平台不能创建),可以从本站下载该文件,但该文件仅配置了隐藏 index.php 的规则,。
四、更改项目配置文件
编辑项目配置文件 Conf/config.php ,将 URL 模式配置为 2(Rewrite模式):
'URL_MODEL'=&2,
至此,各个配置已经完成。保存各配置文件后,重启 Apache 服务器并删除 Runtime 目录下的项目缓存文件,在浏览器访问隐藏 index.php 后的地址测试是否成功:
http://www.5idev.com/html/myapp/Index/index
如果访问成功,那么利用 Apache .htaccess 文件的 Rewrite 规则隐藏 index.php 入口文件的配置就成功了。
本章节内容共分 2 部分:1.
ThinkPHP 利用.htaccess文件的 Rewrite 规则隐藏URL中的 index.php2.
5idev.com(我爱开发网) — 提供最好的 、、、 及

我要回帖

更多关于 dz 伪静态 目录化 的文章

 

随机推荐