node初体验(二)
1.静态资源访问,需要设置路由和响应标头
2.url模块、path模块、querystring模块
Url {
protocol: null,
slashes: null,
auth: null,
host: null,
port: null,
hostname: null,
hash: null,
search: '?id=10021',
query: 'id=10021',
pathname: '/a.html',
path: '/a.html?id=10021',
href: '/a.html?id=10021'
}
- url.parse(,true).query 可以以对象形式输出查询参数
- path.parse 可以获得拓展名
- querystring 将查询参数字符串转成对象形式
MIME
是设定某种扩展名的文件用一种应用程序来打开的方式类型,当该扩展名文件被访问的时候,浏览器会自动使用指定应用程序来打开
{
".html": "text/html" ,
".png": "image/png",
".jpeg": "image/jpeg" ,
".jpg": "image/jpeg",
".json": "application/json",
".ico": "image/x-icon" ,
".svg": "image/svg+xml",
".mp4": "video/mp4" ,
".mp3":"audio/mpeg",
".avi": "video/x-msvideo",
".docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
".doc": "application/msword",
".xls": "application/vnd.ms-excel" ,
".xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" ,
".css": "text/css" ,
".js": "application/x-javascript"
}
3.根据拓展名匹配对应类型的标头
var pathname=url.parse(req.url).pathname
var extname=path.extname(pathname)
if(mine_json.hasOwnProperty(extname))
{
res.setHeader("content-type",mime[extname])
}
- 重定向
res.writeHead(302,{"Location":pathname+"/index.html"})
4.serve-static资源静态化
npm install serve-static
npm install finalhandler
var finalhandler = require('finalhandler')
var http = require('http')
var serveStatic = require('serve-static')
var serve = serveStatic('public', { 'index': ['index.html', 'index.htm'] })
var server = http.createServer(function onRequest(req, res) {
serve(req, res, finalhandler(req, res))
})
server.listen(3000)
5.formidable实现对get和set的封装或者文件上传等
6.app.use和请求方式无关
app.use("/use/:id".function(req,res,next){
console.log("中间件");
res.send("OK")
})
app.get("/use/:id".function(req,res,next){
console.log("中间件");
res.send("OK")
})
7.应用层中间件感觉和路由中间件差不多,路由中间件最后还要绑定到应用层中间件,但是当路由多了以后,再把路由写在一个app里面就不合适了,使用用路由中间件拓展性更好。
8.错误处理中间件
app.use(function (err, req, res, next) {
console.error(err.stack)
res.status(500).send('Something broke!')
})
9.rjs模板引擎的使用
var express = require("express")
var fs = require("fs")
var path = require("path")
var app = express()
//设置默认的模板引擎
app.set("view engine", "ejs")
app.get("/", function (req, res, next) {
res.render("deptinfo", {
"deptname": "IT",
"deptno": "001",
"emps": [
{ "name": "TOM", "age": 30 },
{ "name": "JACK", "age": 35 },
{ "name": "ROSE", "age": 20 },
]
})
})
//静态资源处理
// app.use('/static', express.static('public'))
app.use('/static', express.static(path.join(__dirname, 'public')))
app.use(express.static('uploads'))
app.listen(3000, () => {
console.log("服务已启动。port:3000")
})
node初体验(二)的更多相关文章
- 【原创】Jquery初体验二
快速导航 一.传统方式生成Table 二.使用jquery.tmpl插件快速生成Table 三.Jquery中的操作class的几个方法 四:jq里面的克隆 五:属性过滤器 六:表单元素过滤器 一.传 ...
- Ruby on rails初体验(二)
体验一中添加了一个最基本的支架和一个简单的数据迁移,实现了一个基本的增删改查的功能列表.体验二中要在次功能上继续丰满一下功能.实现如下效果: 在每个公司中都包含有不同的部门,按照体验一中的方法,添加一 ...
- Spring Cloud Alibaba 初体验(二) Nacos 服务注册与发现 + 集成 Spring Cloud Gateway
一.服务注册 添加依赖: <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>s ...
- node初体验(一)
1.node.js是一个构建在chrome V8引擎上的javascript运行环境 2.node.js特点:单线程.事件驱动.非阻塞IO模型.轻量 3.node.js是单线程的(多个请求都是一个线程 ...
- jquery.fn.extend与jquery.extend--(初体验二)
1.jquery.extend(object); 为扩展jQuery类本身.为类添加新的方法. jquery.fn.extend(object);给jQuery对象添加方法. $.extend({ a ...
- JSON初体验(二):Gson解析
今天,我们来介绍一下Gson的jar包的用法. JSON解析之Gson 特点:编码简介,谷歌官方推荐 数据之间的转换: 1.将json格式的字符串{}转换成为java对象 API: <T> ...
- node.js + express 初体验【hello world】
[node.js] 一个神奇的XX 呵呵 :) 不知道怎么形容他才好! [express] 是node.js 开发web应用程序的框架 开发环境:XP 大家共同进步吧 :) 一:前期准备: 1:下载 ...
- Node.js 网页瘸腿爬虫初体验
延续上一篇,想把自己博客的文档标题利用Node.js的request全提取出来,于是有了下面的初哥爬虫,水平有限,这只爬虫目前还有点瘸腿,请看官你指正了. // 内置http模块,提供了http服务器 ...
- Python导出Excel为Lua/Json/Xml实例教程(二):xlrd初体验
Python导出Excel为Lua/Json/Xml实例教程(二):xlrd初体验 相关链接: Python导出Excel为Lua/Json/Xml实例教程(一):初识Python Python导出E ...
随机推荐
- GIS基本概念,空间分析
GIS基本概念,空间分析 一.GIS基本概念 1.1 要素模型(Feature) 1.2 矢量数据 1.3 空间分析 1.3.1 空间查询和空间量算 1.3.2 缓冲区分析 1.3.3 叠加分析 1. ...
- C++类基本--随笔一
#include <iostream> using namespace std; class Teacher { public: Teacher(int m=3,int n=2) { a= ...
- apache和LAMP架构
资源池: httpd依赖包:apr 和 apr-util 下载:点击这里 httpd 下载:点击这里 mysql 下载:点击这里 php 下载: 点击这里 本章资源: 点击这里 资源提取码:u2jv ...
- 华三交换机NTP配置
clock protocol ntp ntp-service enable ntp-service unicast-server x.x.x.x clock timezone beijing add ...
- TCP/IP__TCP协议常用协议默认端口号
- python爬虫selenium相关
首先上很好用的selenium中文文档,基本上所有问题都能通过阅读此文档解决.可惜好像没找到翻译者名称. https://python-selenium-zh.readthedocs.io/zh_CN ...
- SP3267 DQUERY - D-query 莫队板子题
题意可见:https://www.luogu.com.cn/problem/SP3267 可在vj上提交:https://vjudge.net/problem/SPOJ-DQUERY 题意翻译 给出一 ...
- poj3415 Common Substrings (后缀数组+单调队列)
Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 9414 Accepted: 3123 Description A sub ...
- Codeforces Round #695 (Div. 2) C. Three Bags (贪心,思维)
题意:有三个背包,每个背包里都用一些数字,你可以选择某一个背包的数字\(a\),从另外一个背包拿出\(b\)(拿出之后就没有了),然后将\(a\)替换为\(a-b\),你可以进行任意次这样的操作,使得 ...
- L2-019 悄悄关注 (25分) map容器模拟
代码: 1 //一道模拟水题,就用来给map练手吧 2 #include<stdio.h> 3 #include<string.h> 4 #include<iostrea ...